-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
145 lines (118 loc) · 3.03 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
/*
* Copyright 2014–2018 Kullo GmbH
*
* This source code is licensed under the 3-clause BSD license. See LICENSE.txt
* in the root directory of this source tree for details.
*/
package main
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"testing"
)
type ResponseWriterStub struct {
Status int
BytesWritten int
}
func (rws *ResponseWriterStub) Header() http.Header {
return http.Header{}
}
func (rws *ResponseWriterStub) Write(bytes []byte) (int, error) {
rws.BytesWritten += len(bytes)
return len(bytes), nil
}
func (rws *ResponseWriterStub) WriteHeader(status int) {
rws.Status = status
}
func TestStatus(t *testing.T) {
var body bytes.Buffer
req, err := http.NewRequest("GET", "/status", &body)
if err != nil {
t.Fatal("http.NewRequest")
}
rw := ResponseWriterStub{}
statusHandler(&rw, req)
if rw.Status != http.StatusOK {
t.Error("Status is", rw.Status)
}
}
func TestUploadHandlerFailsOnGet(t *testing.T) {
var body bytes.Buffer
req, err := http.NewRequest("GET", "/upload", &body)
if err != nil {
t.Fatal("http.NewRequest")
}
rw := ResponseWriterStub{}
uploadHandler(&rw, req)
if rw.Status != http.StatusBadRequest {
t.Error("Status is", rw.Status)
}
}
func TestUploadHandlerFailsOnMissingContentType(t *testing.T) {
var body bytes.Buffer
req, err := http.NewRequest("POST", "/upload", &body)
if err != nil {
t.Fatal("http.NewRequest")
}
rw := ResponseWriterStub{}
uploadHandler(&rw, req)
if rw.Status != http.StatusBadRequest {
t.Error("Status is", rw.Status)
}
}
func TestUploadHandlerFailsOnWrongContentType(t *testing.T) {
var body bytes.Buffer
req, err := http.NewRequest("POST", "/upload", &body)
if err != nil {
t.Fatal("http.NewRequest")
}
req.Header.Set("Content-Type", "text/html")
rw := ResponseWriterStub{}
uploadHandler(&rw, req)
if rw.Status != http.StatusBadRequest {
t.Error("Status is", rw.Status)
}
}
func TestUploadHandlerFailsOnMissingFile(t *testing.T) {
var body bytes.Buffer
mpWriter := multipart.NewWriter(&body)
req, err := http.NewRequest("POST", "/upload", &body)
if err != nil {
t.Fatal("http.NewRequest")
}
req.Header.Set("Content-Type", mpWriter.FormDataContentType())
rw := ResponseWriterStub{}
uploadHandler(&rw, req)
if rw.Status != http.StatusBadRequest {
t.Error("Status is", rw.Status)
}
}
func TestUploadHandlerSucceedsOnUpload(t *testing.T) {
file := bytes.NewBufferString("I am a minidump")
var body bytes.Buffer
mpWriter := multipart.NewWriter(&body)
formFile, err := mpWriter.CreateFormFile("upload_file_minidump", "test.dmp")
if err != nil {
t.Fatal("mpWriter.CreateFormFile")
}
_, err = io.Copy(formFile, file)
if err != nil {
t.Fatal("io.Copy")
}
mpWriter.Close()
req, err := http.NewRequest("POST", "/upload", &body)
if err != nil {
t.Fatal("http.NewRequest")
}
req.Header.Set("Content-Type", mpWriter.FormDataContentType())
rw := ResponseWriterStub{}
uploadHandler(&rw, req)
if rw.Status != http.StatusOK {
t.Error("Status is", rw.Status)
}
if rw.BytesWritten == 0 {
t.Error("Received empty response")
}
}