This repository has been archived by the owner on May 2, 2020. It is now read-only.
forked from stretchr/goweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goweb_http_responder_test.go
113 lines (69 loc) · 2.92 KB
/
goweb_http_responder_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
package responders
import (
"github.com/tylerb/goweb/context"
context_test "github.com/tylerb/goweb/webcontext/test"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
func TestHTTP_Interface(t *testing.T) {
assert.Implements(t, (*HTTPResponder)(nil), new(GowebHTTPResponder))
}
func TestHTTP_With(t *testing.T) {
http := new(GowebHTTPResponder)
ctx := context_test.MakeTestContext()
http.With(ctx, 200, []byte("Hello Goweb"))
assert.Equal(t, context_test.TestResponseWriter.Output, "Hello Goweb")
assert.Equal(t, context_test.TestResponseWriter.StatusCode, 200)
}
func TestHTTP_WithStatus(t *testing.T) {
httpResponder := new(GowebHTTPResponder)
ctx := context_test.MakeTestContext()
httpResponder.WithStatus(ctx, 500)
assert.Equal(t, context_test.TestResponseWriter.StatusCode, 500)
}
func TestHTTP_WithStatusAndText(t *testing.T) {
httpResponder := new(GowebHTTPResponder)
ctx := context_test.MakeTestContext()
httpResponder.WithStatusText(ctx, 500)
assert.Equal(t, context_test.TestResponseWriter.StatusCode, 500)
assert.Equal(t, context_test.TestResponseWriter.Output, http.StatusText(500))
}
// https://github.com/tylerb/goweb/issues/30
func TestHTTP_WithStatus_WithAlways200(t *testing.T) {
httpResponder := new(GowebHTTPResponder)
var ctx context.Context
ctx = context_test.MakeTestContextWithPath("people/123?always200=true")
httpResponder.WithStatus(ctx, 500)
assert.Equal(t, context_test.TestResponseWriter.StatusCode, 200)
ctx = context_test.MakeTestContextWithPath("people/123?always200=1")
httpResponder.WithStatus(ctx, 500)
assert.Equal(t, context_test.TestResponseWriter.StatusCode, 200)
}
func TestHTTP_WithOK(t *testing.T) {
httpResponder := new(GowebHTTPResponder)
ctx := context_test.MakeTestContext()
httpResponder.WithOK(ctx)
assert.Equal(t, context_test.TestResponseWriter.StatusCode, http.StatusOK)
}
func TestHTTP_WithRedirect(t *testing.T) {
httpResponder := new(GowebHTTPResponder)
ctx := context_test.MakeTestContext()
httpResponder.WithRedirect(ctx, "people/123")
assert.Equal(t, context_test.TestResponseWriter.StatusCode, http.StatusFound)
assert.Equal(t, context_test.TestResponseWriter.Header()["Location"][0], "people/123")
}
func TestHTTP_WithTemporaryRedirect(t *testing.T) {
httpResponder := new(GowebHTTPResponder)
ctx := context_test.MakeTestContext()
httpResponder.WithTemporaryRedirect(ctx, "people/123")
assert.Equal(t, context_test.TestResponseWriter.StatusCode, http.StatusTemporaryRedirect)
assert.Equal(t, context_test.TestResponseWriter.Header()["Location"][0], "people/123")
}
func TestHTTP_WithPermanentRedirect(t *testing.T) {
httpResponder := new(GowebHTTPResponder)
ctx := context_test.MakeTestContext()
httpResponder.WithPermanentRedirect(ctx, "people/123")
assert.Equal(t, context_test.TestResponseWriter.StatusCode, http.StatusMovedPermanently)
assert.Equal(t, context_test.TestResponseWriter.Header()["Location"][0], "people/123")
}