forked from gorilla/http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
116 lines (103 loc) · 2.33 KB
/
http_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
114
115
116
package http
import (
"bytes"
"errors"
"fmt"
"io"
"net"
"net/http"
"strings"
"testing"
)
var localhost = &net.TCPAddr{
IP: net.IP{127, 0, 0, 1},
Port: 0, // os assigned
}
type server struct {
*testing.T
net.Listener
}
// Shutdown should be called to terminate this server.
func (s *server) Shutdown() {
s.Listener.Close()
}
// Root returns a http URL for the root of this server.
func (s *server) Root() string {
return fmt.Sprintf("http://%s", s.Listener.Addr().String())
}
// starts a new net/http http server
func newServer(t *testing.T, mux *http.ServeMux) *server {
l, err := net.ListenTCP("tcp4", localhost)
if err != nil {
t.Fatal(err)
}
// /404 is not handled, generating a 404
go func() {
if err := http.Serve(l, mux); err != nil {
// t.Error(err)
}
}()
return &server{t, l}
}
func sameErr(a, b error) bool {
if a != nil && b != nil {
return a.Error() == b.Error()
}
return a == b
}
func TestInternalHttpServer(t *testing.T) {
newServer(t, nil).Shutdown()
}
func a() string {
var a string
for i := 0; i < 1024; i++ {
a += "aaaaaaaa"
}
return a
}
var getTests = []struct {
path string
expected string
err error
}{
{"/200", "OK", nil},
{"/%2f", "", errors.New("404 Not Found")}, // issue #1
{"/404", "", errors.New("404 Not Found")},
{"/a", a(), nil}, // triggers chunked encoding
}
func TestGet(t *testing.T) {
s := newServer(t, stdmux())
defer s.Shutdown()
for _, tt := range getTests {
url := s.Root() + tt.path
var b bytes.Buffer
n, err := Get(&b, url)
if actual := b.String(); actual != tt.expected || n != int64(len(tt.expected)) || !sameErr(err, tt.err) {
t.Errorf("Get(%q): expected %q %v, got %q %v", tt.path, tt.expected, tt.err, actual, err)
}
}
}
var postTests = []struct {
path string
body func() io.Reader
err error
}{
{"/201", func() io.Reader { return strings.NewReader(postBody) }, nil},
{"/201", func() io.Reader { return strings.NewReader("wrong") }, errors.New("400 Bad Request")},
{"/404", nil, errors.New("404 Not Found")},
}
func TestPost(t *testing.T) {
s := newServer(t, stdmux())
defer s.Shutdown()
for _, tt := range postTests {
url := s.Root() + tt.path
var body io.Reader
if tt.body != nil {
body = tt.body()
}
err := Post(url, body)
if !sameErr(err, tt.err) {
t.Errorf("Post(%q): expected %v, got %v", tt.path, tt.err, err)
}
}
}