This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verbmux_test.go
61 lines (51 loc) · 1.67 KB
/
verbmux_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
// Copyright 2017 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.
package verbmux_test
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"mellium.im/verbmux"
)
type testCase struct {
mux http.Handler
verb string
resp int
body string
}
const helloWorldBody = "<html><body>Hello World!</body></html>"
var helloWorld = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, helloWorldBody)
})
var testCases = [...]testCase{
0: {verbmux.New(), "GET", 405, "Method not allowed"},
1: {verbmux.New(), "OPTIONS", 200, ""},
2: {verbmux.New(verbmux.Options(nil)), "OPTIONS", 405, "Method not allowed"},
3: {verbmux.New(verbmux.Options(helloWorld)), "OPTIONS", 200, helloWorldBody},
4: {verbmux.New(verbmux.Get(helloWorld)), "GET", 200, helloWorldBody},
5: {verbmux.New(verbmux.Post(helloWorld)), "POST", 200, helloWorldBody},
6: {verbmux.New(verbmux.Put(helloWorld)), "PUT", 200, helloWorldBody},
7: {verbmux.New(verbmux.Delete(helloWorld)), "DELETE", 200, helloWorldBody},
}
func TestVerbMux(t *testing.T) {
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
req := httptest.NewRequest(tc.verb, "http://example.com/foo", nil)
w := httptest.NewRecorder()
tc.mux.ServeHTTP(w, req)
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != tc.resp {
t.Errorf("Got invalid status code: want=%d, got=%d", tc.resp, resp.StatusCode)
}
if strings.TrimSpace(string(body)) != tc.body {
t.Errorf("Got invalid body: want=`%s`, got=`%s`", tc.body, body)
}
})
}
}