Тестирование промежуточного программного обеспечения golang, которое модифицирует запрос - PullRequest
0 голосов
/ 06 июля 2018

У меня есть промежуточное ПО, которое добавляет к запросу контекст с идентификатором запроса.

func AddContextWithRequestID(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    var ctx context.Context
    ctx = NewContextWithRequestID(ctx, r)
    next.ServeHTTP(w, r.WithContext(ctx))
})}

Как мне написать тест для этого?

1 Ответ

0 голосов
/ 06 июля 2018

Чтобы проверить это, вам нужно запустить этот обработчик, передающий запрос и использующий пользовательский обработчик next, который проверяет, действительно ли запрос был изменен.

Вы можете создать этот обработчик следующим образом:

(я предполагаю, что ваш NewContextWithRequestID добавляет ключ "reqId" к запросу со значением "1234", вы, конечно, должны при необходимости изменить утверждения)

// create a handler to use as "next" which will verify the request
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    val := r.Context().Value("reqId")
    if val == nil {
        t.Error("reqId not present")
    }
    valStr, ok := val.(string)
    if !ok {
        t.Error("not string")
    }
    if valStr != "1234" {
        t.Error("wrong reqId")
    }
})

Затем вы можете использовать этот обработчик как свой next:

// create the handler to test, using our custom "next" handler
handlerToTest := AddContextWithRequestID(nextHandler)

А затем вызовите этот обработчик:

// create a mock request to use
req := httptest.NewRequest("GET", "http://testing", nil)
// call the handler using a mock response recorder (we'll not use that anyway)
handlerToTest.ServeHTTP(httptest.NewRecorder(), req)

Собираем все вместе в качестве рабочего теста, это будет код ниже.

Примечание: Я исправил небольшую ошибку в вашем исходном "AddContextWithRequestID", поскольку значение ctx начиналось со значения nil, когда вы только что объявили его без инициализации.

import (
    "net/http"
    "context"
    "testing"
    "net/http/httptest"
)

func NewContextWithRequestID(ctx context.Context, r *http.Request) context.Context {
    return context.WithValue(ctx, "reqId", "1234")
}

func AddContextWithRequestID(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        var ctx = context.Background()
        ctx = NewContextWithRequestID(ctx, r)
        next.ServeHTTP(w, r.WithContext(ctx))
    })
}

func TestIt(t *testing.T) {

    // create a handler to use as "next" which will verify the request
    nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        val := r.Context().Value("reqId")
        if val == nil {
            t.Error("reqId not present")
        }
        valStr, ok := val.(string)
        if !ok {
            t.Error("not string")
        }
        if valStr != "1234" {
            t.Error("wrong reqId")
        }
    })

    // create the handler to test, using our custom "next" handler
    handlerToTest := AddContextWithRequestID(nextHandler)

    // create a mock request to use
    req := httptest.NewRequest("GET", "http://testing", nil)

    // call the handler using a mock response recorder (we'll not use that anyway)
    handlerToTest.ServeHTTP(httptest.NewRecorder(), req)
}
...