-
Notifications
You must be signed in to change notification settings - Fork 6
/
main_test.go
158 lines (127 loc) · 3.92 KB
/
main_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"time"
)
// Integration test
func TestMain(t *testing.T) {
t.Run("complete PUT-GET-DELETE-GET cycle", func(t *testing.T) {
// Run the application
srvAddr := "localhost:29109"
os.Setenv("HOST", srvAddr)
defer os.Unsetenv("HOST")
go func() {
main()
}()
// Make sure that the http listener is in place
time.Sleep(time.Millisecond)
// Define the data that will be sent and the expected
targetEndpoint := "http://" + srvAddr + "/endpoint1"
expectedBody := "yay"
expectedContentType := "WUARGH"
// Perform the PUT call
var client http.Client
req, _ := http.NewRequest("PUT", targetEndpoint, strings.NewReader(expectedBody))
req.Header.Set("Content-Type", expectedContentType)
res, err := client.Do(req)
if err != nil {
t.Fatalf("calling PUT: %v", err)
}
// Test the PUT response code
if want, have := 200, res.StatusCode; want != have {
t.Errorf("expected PUT response status code %d, found %d", want, have)
}
// Perform the GET call
res, err = http.Get(targetEndpoint)
if err != nil {
t.Fatalf("calling GET: %v", err)
}
// Test the GET response code
if want, have := 200, res.StatusCode; want != have {
t.Errorf("expected GET response status code %d, found %d", want, have)
}
// Test the GET response body
body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("reading the GET response body: %v", err)
}
if want, have := expectedBody, string(body); want != have {
t.Errorf("expected GET response body %q, found %q", want, have)
}
// Test the GET response content-type
if want, have := expectedContentType, res.Header.Get("Content-Type"); want != have {
t.Errorf("expected GET response content-type %q, found %q", want, have)
}
// Perform the DELETE call
req, _ = http.NewRequest("DELETE", targetEndpoint, nil)
res, err = client.Do(req)
if err != nil {
t.Fatalf("calling DELETE: %v", err)
}
// Test the DELETE response code
if want, have := 204, res.StatusCode; want != have {
t.Errorf("expected DELETE response status code %d, found %d", want, have)
}
// Perform a GET call to check if the resource has been deleted
res, err = http.Get(targetEndpoint)
if err != nil {
t.Fatalf("calling GET: %v", err)
}
// Test the GET response code
if want, have := 404, res.StatusCode; want != have {
t.Errorf("expected GET response status code %d, found %d", want, have)
}
})
t.Run("OPTIONS call", func(t *testing.T) {
// Run the application
srvAddr := "localhost:29108"
os.Setenv("HOST", srvAddr)
defer os.Unsetenv("HOST")
go func() {
main()
}()
// Make sure that the http listener is in place
time.Sleep(time.Millisecond)
// Define the data that will be sent and the expected
targetEndpoint := "http://" + srvAddr + "/endpoint2"
// Perform the OPTIONS call
var client http.Client
req, _ := http.NewRequest("OPTIONS", targetEndpoint, nil)
res, err := client.Do(req)
if err != nil {
t.Fatalf("calling OPTIONS: %v", err)
}
// Test the response code
if want, have := 204, res.StatusCode; want != have {
t.Errorf("expected response status code %d, found %d", want, have)
}
})
t.Run("POST call not implemented", func(t *testing.T) {
// Run the application
srvAddr := "localhost:29110"
os.Setenv("HOST", srvAddr)
defer os.Unsetenv("HOST")
go func() {
main()
}()
// Make sure that the http listener is in place
time.Sleep(time.Millisecond)
// Define the data that will be sent and the expected
targetEndpoint := "http://" + srvAddr + "/endpoint3"
// Perform the OPTIONS call
var client http.Client
req, _ := http.NewRequest("POST", targetEndpoint, nil)
res, err := client.Do(req)
if err != nil {
t.Fatalf("calling POST: %v", err)
}
// Test the response code
if want, have := 501, res.StatusCode; want != have {
t.Errorf("expected response status code %d, found %d", want, have)
}
})
}