Debian Codegraph @

handlers_test.go

raw
1 // Copyright 2013 The Gorilla Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package handlers
6
7 import (
8 "io"
9 "log"
10 "net/http"
11 "net/http/httptest"
12 "net/url"
13 "strings"
14 "testing"
15 )
16
17 const (
18 ok = "ok\n"
19 notAllowed = "Method not allowed\n"
20 )
21
22 var okHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
23 _, err := w.Write([]byte(ok))
24 if err != nil {
25 log.Fatalf("error on writing to http.ResponseWriter: %v", err)
26 }
27 })
28
29 func newRequest(method, url string) *http.Request {
30 req, err := http.NewRequest(method, url, nil)
31 if err != nil {
32 panic(err)
33 }
34 return req
35 }
36
37 func TestMethodHandler(t *testing.T) {
38 tests := []struct {
39 req *http.Request
40 handler http.Handler
41 code int
42 allow string // Contents of the Allow header
43 body string
44 }{
45 // No handlers
46 {newRequest(http.MethodGet, "/foo"), MethodHandler{}, http.StatusMethodNotAllowed, "", notAllowed},
47 {newRequest(http.MethodOptions, "/foo"), MethodHandler{}, http.StatusOK, "", ""},
48
49 // A single handler
50 {newRequest(http.MethodGet, "/foo"), MethodHandler{http.MethodGet: okHandler}, http.StatusOK, "", ok},
51 {newRequest(http.MethodPost, "/foo"), MethodHandler{http.MethodGet: okHandler}, http.StatusMethodNotAllowed, http.MethodGet, notAllowed},
52
53 // Multiple handlers
54 {newRequest(http.MethodGet, "/foo"), MethodHandler{http.MethodGet: okHandler, http.MethodPost: okHandler}, http.StatusOK, "", ok},
55 {newRequest(http.MethodPost, "/foo"), MethodHandler{http.MethodGet: okHandler, http.MethodPost: okHandler}, http.StatusOK, "", ok},
56 {newRequest(http.MethodDelete, "/foo"), MethodHandler{http.MethodGet: okHandler, http.MethodPost: okHandler}, http.StatusMethodNotAllowed, "GET, POST", notAllowed},
57 {newRequest(http.MethodOptions, "/foo"), MethodHandler{http.MethodGet: okHandler, http.MethodPost: okHandler}, http.StatusOK, "GET, POST", ""},
58
59 // Override OPTIONS
60 {newRequest(http.MethodOptions, "/foo"), MethodHandler{http.MethodOptions: okHandler}, http.StatusOK, "", ok},
61 }
62
63 for i, test := range tests {
64 rec := httptest.NewRecorder()
65 test.handler.ServeHTTP(rec, test.req)
66 resp := rec.Result()
67 if resp.StatusCode != test.code {
68 t.Fatalf("%d: wrong code, got %d want %d", i, resp.StatusCode, test.code)
69 }
70 if allow := resp.Header.Get("Allow"); allow != test.allow {
71 t.Fatalf("%d: wrong Allow, got %s want %s", i, allow, test.allow)
72 }
73
74 respBodyBytes, err := io.ReadAll(resp.Body)
75 if err != nil {
76 t.Errorf("io error while reading response body %v", err)
77 }
78 if body := string(respBodyBytes); body != test.body {
79 t.Fatalf("%d: wrong body, got %q want %q", i, body, test.body)
80 }
81 }
82 }
83
84 func TestContentTypeHandler(t *testing.T) {
85 tests := []struct {
86 Method string
87 AllowContentTypes []string
88 ContentType string
89 Code int
90 }{
91 {http.MethodPost, []string{"application/json"}, "application/json", http.StatusOK},
92 {http.MethodPost, []string{"application/json", "application/xml"}, "application/json", http.StatusOK},
93 {http.MethodPost, []string{"application/json"}, "application/json; charset=utf-8", http.StatusOK},
94 {http.MethodPost, []string{"application/json"}, "application/json+xxx", http.StatusUnsupportedMediaType},
95 {http.MethodPost, []string{"application/json"}, "text/plain", http.StatusUnsupportedMediaType},
96 {http.MethodGet, []string{"application/json"}, "", http.StatusOK},
97 {http.MethodGet, []string{}, "", http.StatusOK},
98 }
99 for _, test := range tests {
100 r, err := http.NewRequest(test.Method, "/", nil)
101 if err != nil {
102 t.Error(err)
103 continue
104 }
105
106 h := ContentTypeHandler(okHandler, test.AllowContentTypes...)
107 r.Header.Set("Content-Type", test.ContentType)
108 w := httptest.NewRecorder()
109 h.ServeHTTP(w, r)
110 if w.Code != test.Code {
111 t.Errorf("expected %d, got %d", test.Code, w.Code)
112 }
113 }
114 }
115
116 func TestHTTPMethodOverride(t *testing.T) {
117 tests := []struct {
118 Method string
119 OverrideMethod string
120 ExpectedMethod string
121 }{
122 {http.MethodPost, http.MethodPut, http.MethodPut},
123 {http.MethodPost, http.MethodPatch, http.MethodPatch},
124 {http.MethodPost, http.MethodDelete, http.MethodDelete},
125 {http.MethodPut, http.MethodDelete, http.MethodPut},
126 {http.MethodGet, http.MethodGet, http.MethodGet},
127 {http.MethodHead, http.MethodHead, http.MethodHead},
128 {http.MethodGet, http.MethodPut, http.MethodGet},
129 {http.MethodHead, http.MethodDelete, http.MethodHead},
130 }
131
132 for _, test := range tests {
133 h := HTTPMethodOverrideHandler(okHandler)
134 reqs := make([]*http.Request, 0, 2)
135
136 rHeader, err := http.NewRequest(test.Method, "/", nil)
137 if err != nil {
138 t.Error(err)
139 }
140 rHeader.Header.Set(HTTPMethodOverrideHeader, test.OverrideMethod)
141 reqs = append(reqs, rHeader)
142
143 f := url.Values{HTTPMethodOverrideFormKey: []string{test.OverrideMethod}}
144 rForm, err := http.NewRequest(test.Method, "/", strings.NewReader(f.Encode()))
145 if err != nil {
146 t.Error(err)
147 }
148 rForm.Header.Set("Content-Type", "application/x-www-form-urlencoded")
149 reqs = append(reqs, rForm)
150
151 for _, r := range reqs {
152 w := httptest.NewRecorder()
153 h.ServeHTTP(w, r)
154 if r.Method != test.ExpectedMethod {
155 t.Errorf("Expected %s, got %s", test.ExpectedMethod, r.Method)
156 }
157 }
158 }
159 }