-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathten_kb_site_test.go
123 lines (108 loc) · 2.36 KB
/
ten_kb_site_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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestCreate10kbFile(t *testing.T) {
path := randSeq(100)
body := "secret"
errorBody := randSeq(10*1000 + 1)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if "/"+path != r.URL.String() {
t.Errorf("wrong path: %s", r.URL)
}
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
if len(bytes) > 10*1000 {
w.WriteHeader(http.StatusUnprocessableEntity)
w.Write([]byte("to long"))
return
}
if string(bytes) != body {
t.Error("body is wrong")
}
w.WriteHeader(http.StatusCreated)
}))
tenKbUpURL = ts.URL + "/"
err := create10kbFile(path, body)
if err != nil {
t.Error(err)
}
err = create10kbFile(path, errorBody)
if err == nil {
t.Error("should have errored")
}
if !strings.Contains(err.Error(), fmt.Sprintf("%d", http.StatusUnprocessableEntity)) {
t.Error(err.Error())
}
}
func TestRead10kbFile(t *testing.T) {
path := randSeq(100)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if "/not-found.txt" == r.URL.String() {
w.WriteHeader(http.StatusNotFound)
return
}
if "/err" == r.URL.String() {
w.WriteHeader(http.StatusInternalServerError)
return
}
if "/"+path != r.URL.String() {
t.Errorf("wrong path: %s", r.URL)
}
w.Write([]byte("body"))
}))
tenKbURL = ts.URL + "/"
status, body, err := read10kbFile(path)
if err != nil {
t.Error(err)
}
if status != http.StatusOK {
t.Error(status)
}
if body != "body" {
t.Error(body)
}
status, body, err = read10kbFile("not-found.txt")
if err != nil {
t.Error(err)
}
if status != http.StatusNotFound {
t.Error(status)
}
if body != "" {
t.Error(body)
}
status, body, err = read10kbFile("err")
if err == nil {
t.Error("should have errored")
}
if !strings.Contains(err.Error(), fmt.Sprintf("%d", http.StatusInternalServerError)) {
t.Error(err.Error())
}
}
func TestPollForResponse(t *testing.T) {
var count int
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
count++
if count < 3 {
w.WriteHeader(http.StatusNotFound)
return
}
w.Write([]byte("body"))
}))
tenKbURL = ts.URL + "/"
body, err := pollForResponse("path")
if body != "body" {
t.Error(body)
}
if err != nil {
t.Error(err)
}
}