-
Notifications
You must be signed in to change notification settings - Fork 6
/
method_test.go
83 lines (72 loc) · 2.19 KB
/
method_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
// Copyright (c) 2016, Janoš Guljaš <[email protected]>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package web
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHandleMethods_KnownMethod(t *testing.T) {
body := "got post"
methods := map[string]http.Handler{
"POST": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(body))
}),
}
contentType := "text/plain"
r := httptest.NewRequest("POST", "/", nil)
w := httptest.NewRecorder()
HandleMethods(methods, body, contentType, w, r)
statusCode := w.Code
if statusCode != http.StatusOK {
t.Errorf("expected status code %d, got %d", http.StatusOK, statusCode)
}
v := w.Body.String()
if v != body {
t.Errorf("expected body %q, got %q", body, v)
}
}
func TestHandleMethods_UnknownMethod(t *testing.T) {
methods := map[string]http.Handler{
"POST": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}),
}
body := http.StatusText(http.StatusMethodNotAllowed)
contentType := "text/plain"
allow := "POST"
r := httptest.NewRequest("", "/", nil)
w := httptest.NewRecorder()
HandleMethods(methods, body, contentType, w, r)
statusCode := w.Code
if statusCode != http.StatusMethodNotAllowed {
t.Errorf("expected status code %d, got %d", http.StatusMethodNotAllowed, statusCode)
}
v := w.Body.String()
if v != body+"\n" {
t.Errorf("expected body %q, got %q", body+"\n", v)
}
v = w.Header().Get("Allow")
if v != allow {
t.Errorf("expected Allow header %q, got %q", allow, v)
}
}
func TestHandleMethods_Options(t *testing.T) {
methods := map[string]http.Handler{
"POST": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}),
}
body := http.StatusText(http.StatusMethodNotAllowed)
contentType := "text/plain"
allow := "POST"
r := httptest.NewRequest("OPTIONS", "/", nil)
w := httptest.NewRecorder()
HandleMethods(methods, body, contentType, w, r)
statusCode := w.Code
if statusCode != http.StatusOK {
t.Errorf("expected status code %d, got %d", http.StatusOK, statusCode)
}
v := w.Header().Get("Allow")
if v != allow {
t.Errorf("expected Allow header %q, got %q", allow, v)
}
}