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
/
testutils_test.go
101 lines (85 loc) · 1.79 KB
/
testutils_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
package main
// This file contains functions and types, that make testing easier
import (
"bytes"
"fmt"
"io"
"net/http"
"strings"
"github.com/ostcar/geiss/asgi"
)
func messageIsRequest(m asgi.Message, r *http.Request) (bool, string) {
if m["method"] != r.Method {
return false, "message and request have a different http method."
}
messageBody := m["body"].([]byte)
requestBody, ok := r.Body.(*testBody)
if !ok {
if len(messageBody) > 0 {
return false, fmt.Sprintf("got an body in the message, but non in the requets: %s", messageBody)
}
} else {
if !bytes.Equal([]byte(requestBody.backup), messageBody) {
return false, "message and request have different body."
}
}
return true, ""
}
type dummyMessanger struct {
message asgi.Message
}
func (r *dummyMessanger) Set(m asgi.Message) error {
r.message = m
return nil
}
func (r *dummyMessanger) Raw() asgi.Message {
return r.message
}
type bigReader int
func (i *bigReader) Read(p []byte) (int, error) {
if int(*i) > len(p) {
*i -= bigReader(len(p))
return len(p), nil
}
v := int(*i)
*i = 0
return v, io.EOF
}
type testBody struct {
body io.Reader
backup string
step int
}
func newTestBody(s string) *testBody {
return &testBody{
body: strings.NewReader(s),
backup: s,
}
}
func newSlowTestBody(s string, step int) *testBody {
return &testBody{
body: strings.NewReader(s),
backup: s,
step: step,
}
}
func (t *testBody) Read(p []byte) (n int, err error) {
if t.step > 0 {
p = p[:t.step]
}
return t.body.Read(p)
}
func (t *testBody) Close() error {
return nil
}
func compareMessageHeader(header1, header2 [][2][]byte) bool {
if len(header1) != len(header2) {
return false
}
for i := range header1 {
if !bytes.Equal(header1[i][0], header1[i][1]) {
return false
}
}
return true
}