-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
117 lines (93 loc) · 3.17 KB
/
router_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
117
// This is a test for the main package
package main
import (
"log"
"testing"
"github.com/valyala/fasthttp"
)
func TestRouter(t *testing.T) {
port := 3000
defer startServerOnPort(t, port, router).Close()
// Perpare a client, which fetches webpages via HTTP proxy listening
// on the localhost:8080.
c := &fasthttp.HostClient{
Addr: "localhost:3000",
}
statusCode, body, err := c.Get(nil, "http://localhost:3000/")
if err != nil {
log.Fatalf("Error when visiting root route: %s", err)
}
// Start our assertions
// 1) We want our status to be 200 (ok)
if statusCode != fasthttp.StatusOK {
log.Fatalf("Unexpected status code: %d. Expecting %d", statusCode, fasthttp.StatusOK)
}
// 2) Check that the response body is what we expect.
expected := `Welcome to Postit`
actual := string(body)
if actual != expected {
t.Errorf("handler returned unexpected body: got %v want %v", actual, expected)
}
}
func TestRouterForNonExistentRoute(t *testing.T) {
port := 3002
defer startServerOnPort(t, port, router).Close()
// Perpare a client, which fetches webpages via HTTP proxy listening
// on the localhost:8080.
c := &fasthttp.HostClient{
Addr: "localhost:3002",
}
statusCode, body, err := c.Get(nil, "http://google.com/foo/bar")
if err != nil {
log.Fatalf("Error when visiting root route: %s", err)
}
// Start our assertions
// 1) We want our status to be 404 (Not found)
if statusCode != 404 {
log.Fatalf("Unexpected status code: %d. Expecting %d", 404, statusCode)
}
// 2) Check that the response body is what we expect.
expected := `Unsupported path`
actual := string(body)
if actual != expected {
t.Errorf("handler returned unexpected body: got %v want %v", actual, expected)
}
// Create an instance of the args struct to client post request
myArgs := &fasthttp.Args{}
// Fetch foobar page via local proxy. Reuse body buffer.
statusCode, body, err = c.Post(nil, "http://foobar.com/google/com", myArgs)
if err != nil {
log.Fatalf("Error when visiting root route: %s", err)
}
// Start our assertions
// 1) We want our status to be 404 (Not found)
if statusCode != 404 {
log.Fatalf("Unexpected status code: %d. Expecting %d", 404, statusCode)
}
}
func TestStaticFileServer(t *testing.T) {
port := 3002
defer startServerOnPort(t, port, router).Close()
// Perpare a client, which fetches webpages via HTTP proxy listening
// on the localhost:8080.
c := &fasthttp.HostClient{
Addr: "localhost:3002",
}
statusCode, _, err := c.Get(nil, "http://localhost:3002/web")
if err != nil {
log.Fatalf("Error when visiting root route: %s", err)
}
// Start our assertions
// 1) We want our status to be 200 (ok)
if statusCode != fasthttp.StatusOK {
log.Fatalf("Unexpected status code: %d. Expecting %d", statusCode, fasthttp.StatusOK)
}
// 2) It isn't wise to test the entire content of the HTML file.
// Instead, we test that the content-type header is "text/html; charset=utf-8"
// so that we know that an html file has been served
// contentType := c.Name
// expectedContentType := "text/html; charset=utf-8"
// if contentType != expectedContentType {
// t.Errorf("Wrong content type, expected %s, got %s", expectedContentType, contentType)
// }
}