-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_test.go
192 lines (181 loc) · 5.94 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"testing"
"time"
)
var testhost = "localhost:8080"
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 := io.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) {
type testcase struct {
url string
filename string
}
testTable := []testcase{
{
url: "http://" + testhost + "/etude/aflat/allintervals/minor2/minor2/minor2/trumpet/on/120/3/0",
filename: "aflat_allintervals_trumpet_on_120_3_0.mid",
},
{
url: "http://" + testhost + "/etude/aflat/intervalpair/minor2/minor2/minor2/trumpet/on/120/1/0",
filename: "intervalpair_minor2_minor2_trumpet_on_120_1_0.mid",
},
}
for _, tcase := range testTable {
resp, err := http.Get(tcase.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, _ := os.ReadFile(tcase.filename)
got, _ := io.ReadAll(resp.Body)
if !bytes.Equal(got, exp) {
t.Errorf("response didn't match the file content")
}
// now test the age check
time.Sleep(time.Duration(expireSeconds) * time.Second)
resp2, err := http.Get(tcase.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, _ = io.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/allintervals/minor2/minor2/minor2/choir_aahs_tenor/off/120/3/0"
resp, err := http.Get(url)
if err != nil {
t.Errorf("GET failed: %v", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %v, got %v", http.StatusOK, resp.StatusCode)
}
exp, _ := os.ReadFile("aflat_allintervals_choir_aahs_tenor_off_120_3_0.mid")
got, _ := io.ReadAll(resp.Body)
if !bytes.Equal(got, exp) {
t.Errorf("response didn't match the file content")
}
}
func TestValidEtudeRequest(t *testing.T) {
badRequests := []etudeRequest{
{tonalCenter: "hsharp", pattern: "pentatonic", instrument: "trumpet", tempo: "120"},
}
for _, req := range badRequests {
ok := validEtudeRequest(req)
if ok {
t.Errorf("request should not have succeeded:\n%v", req)
}
}
goodRequests := []etudeRequest{
{tonalCenter: "", pattern: "intervalpair", interval1: "minor3", interval2: "major3", instrument: "trumpet", metronome: metronomeDownbeatOnly, tempo: "120"},
{tonalCenter: "", pattern: "intervalpair_ud", interval1: "minor3", interval2: "major3", instrument: "trumpet", metronome: metronomeDownbeatOnly, tempo: "120"},
{tonalCenter: "", pattern: "intervaltriple", interval1: "minor3", interval2: "major3", interval3: "minor3", instrument: "trumpet", metronome: metronomeOff, tempo: "120"},
{tonalCenter: "", pattern: "intervaltriple_ud", interval1: "minor3", interval2: "major3", interval3: "minor3", instrument: "trumpet", metronome: metronomeOff, tempo: "120"},
}
for _, req := range goodRequests {
ok := validEtudeRequest(req)
if !ok {
t.Errorf("request should have succeeded:\n%v", req)
}
}
}
func TestBadEtudeRequest(t *testing.T) {
badRequests := []string{
"/etude/c/pentatonic/minor2/minor2/minor2/trumpet/on/120", // no repeat count
"/etude/hsharp/pentatonic/minor2/minor2/minor2/trumpet/on/120/3", // bad tonal center
"/etude/c/schizotonic/minor2/minor2/minor2/trumpet/on/120/3", // bad pattern
"/etude/c/interval/fermented2/minor2/minor2/trumpet/on/120/3", // bad interval1
"/etude/c/intervalpairs/minor2/minor2/toxic2/trumpet/on/120/3", // bad interval2
"/etude/c/pentatonic/minor2/minor2/toxic2/fromixhorn/on/120/3", // bad instrument
"/etude/c/pentatonic/minor2/minor2/minor2/trumpet/jittery/120/3", // bad rhythm
"/etude/c/pentatonic/minor2/minor2/minor2/trumpet/on/allaregretto/3", // bad tempo
}
for _, path := range badRequests {
url := "http://" + testhost + path
resp, err := http.Get(url) // TODO #1 This is returning a nil response.
if err != nil {
t.Errorf("GET failed: %v", err)
continue
} else {
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
// set up a temporary dir for generated files
_ = os.RemoveAll("test") // in case prior run crashed
err := os.Mkdir("test", 0777)
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()
err = os.Chdir(filepath.Join(wd, "test"))
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(-1)
}
expireSeconds = 1
go serveEtudes(testhost) // 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)
}
func BenchmarkMkAllEtudes(b *testing.B) {
req := etudeRequest{instrument: "viola", pattern: "allintervals", tonalCenter: "c"}
for i := 0; i < b.N; i++ {
mkRequestedEtude(48, 84, 120, 15, req)
}
}