-
Notifications
You must be signed in to change notification settings - Fork 2
/
server_test.go
161 lines (149 loc) · 4.06 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"testing"
"time"
)
var testhost = "localhost:8080"
func TestGETIndex(t *testing.T) {
expbytes, _ := ioutil.ReadFile("index.html")
exp := string(expbytes)
url := "http://" + testhost
resp, err := http.Get(url)
if err != nil {
t.Errorf("GET failed: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Error reading response body: %v", err)
}
got := string(body)
if got != exp {
t.Errorf("\nexp: %v\ngot: %v", exp, got)
}
}
func TestMidijsRequest(t *testing.T) {
url := "http://" + testhost + "/midijs/pat/arachno-0.pat"
resp, err := http.Get(url)
if err != nil {
t.Errorf("GET failed: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Error reading response body: %v", err)
}
fmt.Println(len(body))
//exp := 0
//got, err := what()
//if err != nil {
// t.Errorf("describe: %v", err)
//}
//if got != exp {
// t.Errorf("\nexp: %v\ngot: %v", exp, got)
//}
}
func TestGoodEtudeRequest(t *testing.T) {
var err error
url := "http://" + testhost + "/etude/aflat/pentatonic/trumpet/steady"
resp, err := http.Get(url)
if err != nil {
t.Errorf("GET failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %v, got %v", http.StatusOK, resp.StatusCode)
}
exp, _ := ioutil.ReadFile("aflat_pentatonic_trumpet_steady.mid")
got, _ := ioutil.ReadAll(resp.Body)
if !bytes.Equal(got, exp) {
t.Errorf("response didn't match the file content")
}
// now test the age check
maxage, _ := strconv.Atoi(os.Getenv("ETUDE_MAX_AGE"))
maxduration := time.Duration(maxage) * time.Second
time.Sleep(maxduration)
resp2, err := http.Get(url)
if err != nil {
t.Errorf("GET failed: %v", err)
}
defer resp2.Body.Close()
if resp2.StatusCode != http.StatusOK {
t.Errorf("Expected status code %v, got %v", http.StatusOK, resp2.StatusCode)
}
got, _ = ioutil.ReadAll(resp2.Body)
if bytes.Equal(got, exp) { // exp is unchanged and should not match got.
t.Errorf("file did not update")
}
}
func TestVocalEtudeRequest(t *testing.T) {
// because multiple vocal parts are mapped to the same midi number
var err error
url := "http://" + testhost + "/etude/aflat/pentatonic/choir_aahs_tenor/advancing"
resp, err := http.Get(url)
if err != nil {
t.Errorf("GET failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %v, got %v", http.StatusOK, resp.StatusCode)
}
exp, _ := ioutil.ReadFile("aflat_pentatonic_choir_aahs_tenor_advancing.mid")
got, _ := ioutil.ReadAll(resp.Body)
if !bytes.Equal(got, exp) {
t.Errorf("response didn't match the file content")
}
}
func TestBadEtudeRequest(t *testing.T) {
badRequests := []string{
"/etude/hsharp/pentatonic/trumpet",
"/etude/aflat/schizotonic/trumpet",
"/etude/aflat/pentatonic/fromix_horn",
}
for _, path := range badRequests {
url := "http://" + testhost + path
resp, err := http.Get(url)
if err != nil {
t.Errorf("GET failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusBadRequest {
t.Errorf("%s : xpected status code %v, got %v",
path, http.StatusBadRequest, resp.StatusCode)
}
}
}
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
err := os.Mkdir("test", 0777) // set up a temporary dir for generated files
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(-1)
}
//html := []byte("<html></html>")
//err = ioutil.WriteFile("test/index.html", html, 0644)
// Run all tests and clean up
wd, _ := os.Getwd()
midijspath := filepath.Join(wd, "midijs")
err = os.Chdir(filepath.Join(wd, "test"))
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(-1)
}
go serveEtudes(testhost, 1, midijspath) // max etude age = 1 second so we don't wait forever while testing.
exitcode := m.Run()
err = os.Chdir(wd)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(-1)
}
os.RemoveAll("test") // remove the directory and its contents.
os.Exit(exitcode)
}