This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
http_test.go
259 lines (237 loc) · 8.02 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/ostcar/geiss/asgi"
"github.com/ostcar/geiss/asgi/redis"
)
func init() {
channelLayer = redis.NewChannelLayer(0, "", "http_test:", 0)
go globalReceive()
}
func TestCreateResponseReplyChannel(t *testing.T) {
channel1, err := createResponseReplyChannel()
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
if !strings.HasPrefix(channel1, globalChannelname) {
t.Errorf("Expected the channel name to have a prefix, got %s", channel1)
}
channel2, err := createResponseReplyChannel()
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
if channel1 == channel2 {
t.Errorf("Got two times the same channel name: %s and: %s", channel1, channel2)
}
}
func TestForwardHTTPRequest(t *testing.T) {
requests := []*http.Request{
httptest.NewRequest("GET", "http://localhost/", nil),
httptest.NewRequest("GET", "http://localhost/", newTestBody("my body")),
httptest.NewRequest("GET", "http://localhost/", newSlowTestBody("my body", 2)),
httptest.NewRequest("GET", "http://localhost:8000/", nil),
httptest.NewRequest("GET", "https://localhost/", nil),
httptest.NewRequest("GET", "https://localhost:8430/", nil),
httptest.NewRequest("GET", "https://localhost", newTestBody(strings.Repeat("x", 499*1024))),
}
for _, request := range requests {
// Send a request to the channel layer
err := forwardHTTPRequest(request, "some-channel")
if err != nil {
t.Errorf("Did not expect an error, got : %s", err)
}
channel, message, err := channelLayer.Receive([]string{"http.request"}, false)
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
if channel == "" {
t.Errorf("Expected a message on the http.request channel")
}
var d dummyMessanger
err = d.Set(message)
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
if ok, errMessage := messageIsRequest(d.message, request); !ok {
t.Errorf("Expcted the message in the channellayer to be the request: %s", errMessage)
}
if d.message["reply_channel"] != "some-channel" {
t.Errorf(
"Expected the reply channel in the message to be \"some-channel\". message: %v",
d.message,
)
}
if d.message["body_channel"] != "" {
t.Errorf("Expected the body_channel to be empty")
}
}
}
func TestForwardBigHTTPRequest(t *testing.T) {
request := httptest.NewRequest("GET", "https://localhost", newTestBody(strings.Repeat("x", 999*1024)))
err := forwardHTTPRequest(request, "some-channel")
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
channel, message, err := channelLayer.Receive([]string{"http.request"}, false)
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
if channel == "" {
t.Errorf("Expected a message on the http.request channel")
}
var d1 dummyMessanger
err = d1.Set(message)
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
bodyChannel := d1.message["body_channel"].(string)
if bodyChannel == "" {
t.Errorf("Expected more content")
}
channel, message, err = channelLayer.Receive([]string{bodyChannel}, false)
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
if channel == "" {
t.Error("Expected a message on the http.request channel")
}
var d2 dummyMessanger
err = d2.Set(message)
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
if d2.message["more_content"].(bool) {
t.Error("Expected no more content.")
}
fullBody := make([]byte, 0)
fullBody = append(fullBody, d1.message["body"].([]byte)...)
fullBody = append(fullBody, d2.message["content"].([]byte)...)
if !bytes.Equal(fullBody, []byte(request.Body.(*testBody).backup)) {
t.Error("Expected the body of both messages to be the same as the request body.")
}
}
func TestReceiveHTTPResponse(t *testing.T) {
var d1 dummyMessanger
d1.message = make(asgi.Message)
headers := [][2][]byte{[2][]byte{[]byte("MyKey"), []byte("MyValue")}}
d1.message["status"] = 200
d1.message["content"] = []byte("foobar")
d1.message["more_content"] = false
d1.message["headers"] = headers
d1.message["__asgi_channel__"] = globalChannelname + "123"
for _, d := range []dummyMessanger{d1} {
err := channelLayer.Send(globalChannelname, d.Raw())
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
response := httptest.NewRecorder()
err = receiveHTTPResponse(response, globalChannelname+"123")
if err != nil {
t.Fatalf("Did not expect an error, got %s", err)
}
if compareMessageHeader(asgi.ConvertHeader(response.Header()), headers) {
t.Errorf("Received wrong headers, got %v", response.Header())
}
if response.Code != d.message["status"].(int) {
t.Errorf(
"Reveived wrong status code. Expected %d, got %d",
d.message["status"].(int),
response.Code,
)
}
if !bytes.Equal(response.Body.Bytes(), d.message["content"].([]byte)) {
t.Errorf(
"Reveived wrong content. Expected %s, got %s",
d.message["content"].([]byte),
response.Body.Bytes(),
)
}
}
}
func TestReceiveBigHTTPResponse(t *testing.T) {
response := httptest.NewRecorder()
done := make(chan bool)
go func() {
// Start by listning for response. This has to be done in parallel to
// sending the responses.
err := receiveHTTPResponse(response, globalChannelname+"TestReceiveBigHTTPResponse")
if err != nil {
t.Fatalf("Did not expect an error, got %s", err)
}
close(done)
}()
var d1a, d1b dummyMessanger
d1a.message = make(asgi.Message)
d1b.message = make(asgi.Message)
headers := [][2][]byte{[2][]byte{[]byte("OtherKey"), []byte("OtherValue")}}
d1a.message["status"] = 201
d1a.message["content"] = []byte("foobar")
d1a.message["more_content"] = true
d1a.message["headers"] = headers
d1a.message["__asgi_channel__"] = globalChannelname + "TestReceiveBigHTTPResponse"
err := channelLayer.Send(globalChannelname, d1a.Raw())
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
d1b.message["content"] = []byte("even more content")
d1b.message["more_content"] = false
d1b.message["__asgi_channel__"] = globalChannelname + "TestReceiveBigHTTPResponse"
err = channelLayer.Send(globalChannelname, d1b.Raw())
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
// Compare the send messages with the received one.
<-done
if compareMessageHeader(asgi.ConvertHeader(response.Header()), headers) {
t.Errorf("Received wrong headers, got %v", response.Header())
}
if response.Code != d1a.message["status"].(int) {
t.Errorf(
"Reveived wrong status code. Expected %d, got %d",
d1a.message["status"].(int),
response.Code,
)
}
fullContent := d1a.message["content"].([]byte)
fullContent = append(fullContent, d1b.message["content"].([]byte)...)
if !bytes.Equal(response.Body.Bytes(), fullContent) {
t.Errorf("Reveived wrong content. Expected `%s`, got `%s`", fullContent, response.Body.Bytes())
}
}
func TestAsgiHTTPHandler(t *testing.T) {
go func() {
// Test asgi application server, that reads from http.request and returns
// the content.
var request, response dummyMessanger
_, message, err := channelLayer.Receive([]string{"http.request"}, true)
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
err = request.Set(message)
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
response.message = make(asgi.Message)
headers := [][2][]byte{[2][]byte{[]byte("OtherKey"), []byte("OtherValue")}}
replyChannel := request.message["reply_channel"].(string)
response.message["status"] = 200
response.message["content"] = request.message["body"].([]byte)
response.message["more_content"] = false
response.message["headers"] = headers
response.message["__asgi_channel__"] = replyChannel
err = channelLayer.Send(globalChannelname, response.Raw())
if err != nil {
t.Errorf("Did not expect an error, got: %s", err)
}
}()
response := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", strings.NewReader("ping"))
err := asgiHTTPHandler(response, request)
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
}