-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods_test.go
114 lines (101 loc) · 2.79 KB
/
methods_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Tideland Go HTTP Extensions - Unit Tests
//
// Copyright (C) 2020-2022 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package httpx_test // import "tideland.dev/go/httpx"
//--------------------
// IMPORTS
//--------------------
import (
"net/http"
"testing"
"tideland.dev/go/audit/asserts"
"tideland.dev/go/audit/web"
"tideland.dev/go/httpx"
)
//--------------------
// TESTS
//--------------------
// TestMethodHandler tests the wrapping of a handler by a
// MethodHandler.
func TestMethodHandler(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
s := web.NewSimulator(httpx.NewMethodHandler(metaHandler{}))
tests := []struct {
method string
statusCode int
body string
}{
{
method: http.MethodGet,
statusCode: http.StatusBadRequest,
body: "bad request",
}, {
method: http.MethodHead,
statusCode: http.StatusBadRequest,
body: "",
}, {
method: http.MethodPost,
statusCode: http.StatusBadRequest,
body: "bad request",
}, {
method: http.MethodPut,
statusCode: http.StatusOK,
body: "METHOD: PUT!",
}, {
method: http.MethodPatch,
statusCode: http.StatusBadRequest,
body: "bad request",
}, {
method: http.MethodDelete,
statusCode: http.StatusNoContent,
body: "",
}, {
method: http.MethodConnect,
statusCode: http.StatusBadRequest,
body: "bad request",
}, {
method: http.MethodOptions,
statusCode: http.StatusBadRequest,
body: "bad request",
}, {
method: http.MethodTrace,
statusCode: http.StatusBadRequest,
body: "bad request",
},
}
for i, test := range tests {
assert.Logf("test case #%d: %s", i, test.method)
req := s.CreateRequest(test.method, "/", nil)
resp, err := s.Do(req)
assert.NoError(err)
assert.Equal(resp.StatusCode, test.statusCode)
body, err := web.BodyToString(resp)
assert.NoError(err)
assert.Contains(test.body, body)
}
}
//--------------------
// HELPING META HANDLER
//--------------------
// metaHandler provides some of the methods for the MethodHandler
// just for testing.
type metaHandler struct{}
func (h metaHandler) ServeHTTPPut(w http.ResponseWriter, r *http.Request) {
reply := "METHOD: " + r.Method + "!"
w.Header().Add(httpx.HeaderContentType, httpx.ContentTypePlain)
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(reply)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func (h metaHandler) ServeHTTPDelete(w http.ResponseWriter, r *http.Request) {
http.Error(w, "", http.StatusNoContent)
}
func (h metaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, "bad request", http.StatusBadRequest)
}
// EOF