-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
141 lines (133 loc) · 3.31 KB
/
server_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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestErrorHandling(t *testing.T) {
testCases := []struct {
banner string
input string
expectedResponse int
}{
{
// when valid banner and input is passed
banner: "shadow",
input: "this is history",
expectedResponse: http.StatusOK,
},
{
// when an invalid input file is passed
banner: "shadow",
input: "",
expectedResponse: http.StatusBadRequest,
},
{
// when an invalid banner file
banner: "shadwo",
input: "this is history",
expectedResponse: http.StatusInternalServerError,
},
}
for _, tc := range testCases {
form := url.Values{}
form.Add("banner", tc.banner)
form.Add("input-name", tc.input)
request := httptest.NewRequest("POST", "/ascii-art", strings.NewReader(form.Encode()))
request.PostForm = form
responseRecorder := httptest.NewRecorder()
Process(responseRecorder, request)
if responseRecorder.Code != tc.expectedResponse {
t.Errorf("Want status '%d', got '%d'", tc.expectedResponse, responseRecorder.Code)
}
// assert.Equal(t, responseRecorder.Code, tc.expectedResponse)
}
}
func TestPostEndpoint(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(Process))
defer svr.Close()
form := url.Values{}
form.Add("banner", "thinkertoy")
form.Add("input-name", "hello sad world")
request, err := http.NewRequest("POST", svr.URL+"/ascii-art", strings.NewReader(form.Encode()))
if err != nil {
t.Fatal(err)
}
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(request)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Errorf("Want status '%d', got '%d'", 200, resp.StatusCode)
}
}
func TestGetEndpoint(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(Process))
defer svr.Close()
resp, err := http.Get(svr.URL + "/")
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Errorf("Want status '%d', got '%d'", 200, resp.StatusCode)
}
}
func ReadTestFile(arg string) []string {
data, err := ioutil.ReadFile(arg)
if err != nil {
// panic(err)
fmt.Print("lol")
}
contentString := string(data)
contentSplit := strings.Split(contentString, "#")
return contentSplit
}
func TestAsciiArt(t *testing.T) {
testCases := []string{
"hello",
"HELLO",
"HeLlo HuMaN",
"1Hello 2There",
"Hello\\nThere",
"{Hello & There #}",
"hello There 1 to 2!",
"MaD3IrA&LiSboN",
"1a\"#FdwHywR&/()=",
"{|}~",
"[\\]^_ 'a",
"RGB",
":;<=>?@",
"\\!\" #$%&'()*+,-./",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"abcdefghijklmnopqrstuvwxyz",
"nice 2 meet you",
"It's Working",
}
ban := []string{
"standard",
"shadow",
"thinkertoy",
}
filename := []string{
"standardoutput.txt",
"shadowoutput.txt",
"thinkertoyoutput.txt",
}
for i := range ban {
expectedOutput := ReadTestFile(filename[i])
for index, testCase := range testCases {
output, err := AsciiArt(testCase, ban[i]) // test the AsciiArt(input, ban) function
if err != nil {
panic(err)
}
if output != expectedOutput[index] {
t.Errorf("\nTest fails when given case:\n\t\"%s\","+"\nThe test should show:\n%s\nInstead it shows:\n%s", testCase, expectedOutput[index], output)
}
}
}
}