-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
52 lines (46 loc) · 1.24 KB
/
handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package mmdbserver
import (
"bytes"
"io"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMMDBUpdateHandler(t *testing.T) {
modified := time.Unix(1672502400, 0)
handler := &MMDBUpdateHandler{
HomePage: "https://example.com",
MMDBHandler: MMDBHandlerFunc(func(response *Response, request *Request) (err error) {
response.ModTime = modified
if strings.HasPrefix(request.EditionID, "GeoLite2") {
var payload bytes.Buffer
payload.WriteString("example")
_, _ = response.ReadFrom(&payload)
} else if request.EditionID == "Unexpected" {
err = io.ErrUnexpectedEOF
}
return
}),
}
fixtures := []string{
"general",
"invalid-edition-id",
"invalid-account-id",
"invalid-license-key",
"invalid-hash",
"method-not-allowed",
"internal-server-error",
"database-not-found",
"database-latest",
}
for _, name := range fixtures {
request := mustFixtureRequest(name)
actualResponse := httptest.NewRecorder()
handler.ServeHTTP(actualResponse, request)
expectedResponse := mustFixtureResponse(name, request)
assert.Equal(t, expectedResponse.StatusCode, actualResponse.Code, name)
assert.Equal(t, expectedResponse.Header, actualResponse.Header(), name)
}
}