-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponse_test.go
114 lines (88 loc) · 3.2 KB
/
response_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
// Copyright (c) Jeevanandam M (https://github.com/jeevatkm)
// go-aah/ahttp source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package ahttp
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"aahframework.org/essentials.v0"
"aahframework.org/test.v0/assert"
)
func TestHTTPResponseWriter(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
writer := AcquireResponseWriter(w)
defer ReleaseResponseWriter(writer)
writer.WriteHeader(http.StatusOK)
assert.Equal(t, http.StatusOK, writer.Status())
_, _ = writer.Write([]byte("aah framework response writer"))
assert.Equal(t, 29, writer.BytesWritten())
_ = writer.Header()
_ = writer.Unwrap()
}
callAndValidate(t, handler, "aah framework response writer")
}
func TestHTTPNoStatusWritten(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
writer := AcquireResponseWriter(w)
defer ReleaseResponseWriter(writer)
_, _ = writer.Write([]byte("aah framework no status written"))
assert.Equal(t, 31, writer.BytesWritten())
}
callAndValidate(t, handler, "aah framework no status written")
}
func TestHTTPMultipleStatusWritten(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
writer := AcquireResponseWriter(w)
defer ReleaseResponseWriter(writer)
writer.WriteHeader(http.StatusOK)
writer.WriteHeader(http.StatusAccepted)
_, _ = writer.Write([]byte("aah framework mutiple status written"))
assert.Equal(t, 36, writer.BytesWritten())
}
callAndValidate(t, handler, "aah framework mutiple status written")
}
func TestHTTPHijackCall(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
writer := AcquireResponseWriter(w)
con, rw, err := writer.(http.Hijacker).Hijack()
assert.FailOnError(t, err, "")
defer ess.CloseQuietly(con)
bytes := []byte("aah framework calling hijack")
_, _ = rw.WriteString("HTTP/1.1 200 OK\r\n")
_, _ = rw.WriteString("Date: " + time.Now().Format(http.TimeFormat) + "\r\n")
_, _ = rw.WriteString("Content-Type: text/plain; charset=utf-8\r\n")
_, _ = rw.WriteString("Content-Length: " + strconv.Itoa(len(bytes)))
_, _ = rw.WriteString(string(bytes) + "\r\n")
_ = rw.Flush()
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
_, _ = http.Get(server.URL)
}
func TestHTTPCallCloseNotifyAndFlush(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
writer := AcquireResponseWriter(w)
defer ReleaseResponseWriter(writer)
_, _ = writer.Write([]byte("aah framework calling close notify and flush"))
assert.Equal(t, 44, writer.BytesWritten())
writer.(http.Flusher).Flush()
ch := writer.(http.CloseNotifier).CloseNotify()
assert.NotNil(t, ch)
}
callAndValidate(t, handler, "aah framework calling close notify and flush")
}
func callAndValidate(t *testing.T, handler http.HandlerFunc, response string) {
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
resp, err := http.Get(server.URL)
if err != nil {
t.Fatalf("Error Get: %v", err)
}
defer ess.CloseQuietly(resp.Body)
bytes, _ := ioutil.ReadAll(resp.Body)
assert.Equal(t, response, string(bytes))
}