Как издеваться над http.Handler с moq Go? - PullRequest
0 голосов
/ 18 января 2020

Я заинтересован в создании httptest.Server, который просто записывает запросы, с которыми он вызывается. С этой целью я хотел бы использовать библиотеку github.com/matryer/moq.

Чтобы сгенерировать макет для http.Handler, я скопировал его определение в пакет с именем client:

package client

import "net/http"

type Handler interface {
    ServeHTTP(http.ResponseWriter, *http.Request)
}

Затем я запустил

moq -out handler_mock.go . Handler

в каталоге client, что привело к следующему handler_mock.go:

// Code generated by moq; DO NOT EDIT.
// github.com/matryer/moq

package client

import (
    "net/http"
    "sync"
)

var (
    lockHandlerMockServeHTTP sync.RWMutex
)

// Ensure, that HandlerMock does implement Handler.
// If this is not the case, regenerate this file with moq.
var _ Handler = &HandlerMock{}

// HandlerMock is a mock implementation of Handler.
//
//     func TestSomethingThatUsesHandler(t *testing.T) {
//
//         // make and configure a mocked Handler
//         mockedHandler := &HandlerMock{
//             ServeHTTPFunc: func(in1 http.ResponseWriter, in2 *http.Request)  {
//                 panic("mock out the ServeHTTP method")
//             },
//         }
//
//         // use mockedHandler in code that requires Handler
//         // and then make assertions.
//
//     }
type HandlerMock struct {
    // ServeHTTPFunc mocks the ServeHTTP method.
    ServeHTTPFunc func(in1 http.ResponseWriter, in2 *http.Request)

    // calls tracks calls to the methods.
    calls struct {
        // ServeHTTP holds details about calls to the ServeHTTP method.
        ServeHTTP []struct {
            // In1 is the in1 argument value.
            In1 http.ResponseWriter
            // In2 is the in2 argument value.
            In2 *http.Request
        }
    }
}

// ServeHTTP calls ServeHTTPFunc.
func (mock *HandlerMock) ServeHTTP(in1 http.ResponseWriter, in2 *http.Request) {
    if mock.ServeHTTPFunc == nil {
        panic("HandlerMock.ServeHTTPFunc: method is nil but Handler.ServeHTTP was just called")
    }
    callInfo := struct {
        In1 http.ResponseWriter
        In2 *http.Request
    }{
        In1: in1,
        In2: in2,
    }
    lockHandlerMockServeHTTP.Lock()
    mock.calls.ServeHTTP = append(mock.calls.ServeHTTP, callInfo)
    lockHandlerMockServeHTTP.Unlock()
    mock.ServeHTTPFunc(in1, in2)
}

// ServeHTTPCalls gets all the calls that were made to ServeHTTP.
// Check the length with:
//     len(mockedHandler.ServeHTTPCalls())
func (mock *HandlerMock) ServeHTTPCalls() []struct {
    In1 http.ResponseWriter
    In2 *http.Request
} {
    var calls []struct {
        In1 http.ResponseWriter
        In2 *http.Request
    }
    lockHandlerMockServeHTTP.RLock()
    calls = mock.calls.ServeHTTP
    lockHandlerMockServeHTTP.RUnlock()
    return calls
}

Однако, если я попытаюсь запустить следующее client_test.go,

package client

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

func TestHandlerMock(t *testing.T) {
    handlerMock := HandlerMock{
        ServeHTTPFunc: func(w http.ResponseWriter, r *http.Request) {},
    }

    ts := httptest.NewServer(handlerMock)
    defer ts.Close()
}

, я получу

# github.com/kurtpeek/mock-handler/client [github.com/kurtpeek/mock-handler/client.test]
/Users/kurt/go/src/github.com/kurtpeek/mock-handler/client/client_test.go:14:26: cannot use handlerMock (type HandlerMock) as type http.Handler in argument to httptest.NewServer:
    HandlerMock does not implement http.Handler (ServeHTTP method has pointer receiver)
FAIL    github.com/kurtpeek/mock-handler/client [build failed]
Error: Tests failed.

Действительно, если я посмотрю на фактическое определение ServeHTTP (из * 1032) *https://github.com/golang/go/blob/c112289ee4141ebc31db50328c355b01278b987b/src/net/http/server.go#L2008 -L2013 ), ServeHTTP() не имеет приемника указателя:

// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)

// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
    f(w, r)
}

Как это исправить, чтобы сделать тестовый сервер с функцией имитируемого обработчика?

1 Ответ

1 голос
/ 18 января 2020

В сообщении об ошибке говорится, что ваш HandlerMock не реализует интерфейс http.Handler. Однако *HandlerMock это реализует:

func (mock *HandlerMock) ServeHTTP(in1 http.ResponseWriter, in2 *http.Request) 

Попробуйте изменить выражение ts := httptest.NewServer(handlerMock) на ts := httptest.NewServer(&handlerMock)

...