File: options_test.go

package info (click to toggle)
golang-github-zenazn-goji 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 464 kB
  • sloc: makefile: 3
file content (112 lines) | stat: -rw-r--r-- 2,890 bytes parent folder | download | duplicates (3)
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
package middleware

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/zenazn/goji/web"
)

func testOptions(r *http.Request, f func(*web.C, http.ResponseWriter, *http.Request)) *httptest.ResponseRecorder {
	var c web.C

	h := func(w http.ResponseWriter, r *http.Request) {
		f(&c, w, r)
	}
	m := AutomaticOptions(&c, http.HandlerFunc(h))
	w := httptest.NewRecorder()
	m.ServeHTTP(w, r)

	return w
}

var optionsTestEnv = map[interface{}]interface{}{
	web.ValidMethodsKey: []string{
		"hello",
		"world",
	},
}

func TestAutomaticOptions(t *testing.T) {
	t.Parallel()

	// Shouldn't interfere with normal requests
	r, _ := http.NewRequest("GET", "/", nil)
	rr := testOptions(r,
		func(c *web.C, w http.ResponseWriter, r *http.Request) {
			w.Write([]byte{'h', 'i'})
		},
	)
	if rr.Code != http.StatusOK {
		t.Errorf("status is %d, not 200", rr.Code)
	}
	if rr.Body.String() != "hi" {
		t.Errorf("body was %q, should be %q", rr.Body.String(), "hi")
	}
	allow := rr.HeaderMap.Get("Allow")
	if allow != "" {
		t.Errorf("Allow header was set to %q, should be empty", allow)
	}

	// If we respond non-404 to an OPTIONS request, also don't interfere
	r, _ = http.NewRequest("OPTIONS", "/", nil)
	rr = testOptions(r,
		func(c *web.C, w http.ResponseWriter, r *http.Request) {
			c.Env = optionsTestEnv
			w.Write([]byte{'h', 'i'})
		},
	)
	if rr.Code != http.StatusOK {
		t.Errorf("status is %d, not 200", rr.Code)
	}
	if rr.Body.String() != "hi" {
		t.Errorf("body was %q, should be %q", rr.Body.String(), "hi")
	}
	allow = rr.HeaderMap.Get("Allow")
	if allow != "" {
		t.Errorf("Allow header was set to %q, should be empty", allow)
	}

	// Provide options if we 404. Make sure we nom the output bytes
	r, _ = http.NewRequest("OPTIONS", "/", nil)
	rr = testOptions(r,
		func(c *web.C, w http.ResponseWriter, r *http.Request) {
			c.Env = optionsTestEnv
			w.WriteHeader(http.StatusNotFound)
			w.Write([]byte{'h', 'i'})
		},
	)
	if rr.Code != http.StatusOK {
		t.Errorf("status is %d, not 200", rr.Code)
	}
	if rr.Body.Len() != 0 {
		t.Errorf("body was %q, should be empty", rr.Body.String())
	}
	allow = rr.HeaderMap.Get("Allow")
	correctHeaders := "hello, world, OPTIONS"
	if allow != "hello, world, OPTIONS" {
		t.Errorf("Allow header should be %q, was %q", correctHeaders,
			allow)
	}

	// If we somehow 404 without giving a list of valid options, don't do
	// anything
	r, _ = http.NewRequest("OPTIONS", "/", nil)
	rr = testOptions(r,
		func(c *web.C, w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(http.StatusNotFound)
			w.Write([]byte{'h', 'i'})
		},
	)
	if rr.Code != http.StatusNotFound {
		t.Errorf("status is %d, not 404", rr.Code)
	}
	if rr.Body.String() != "hi" {
		t.Errorf("body was %q, should be %q", rr.Body.String(), "hi")
	}
	allow = rr.HeaderMap.Get("Allow")
	if allow != "" {
		t.Errorf("Allow header was set to %q, should be empty", allow)
	}
}