-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_test.go
73 lines (56 loc) · 1.64 KB
/
service_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
package guerillaradio
import "testing"
import "bufio"
import "net"
import "encoding/json"
func TestCanJsonifyLibraryRequest(t *testing.T) {
library := Library{}
err := library.AddSourceDocument("fixtures/oneline.txt")
if err != nil {
t.Errorf("Error while loading library: %v", err)
}
json, err := ServiceRequest(&library, 1)
if err != nil {
t.Errorf("Error while converting json: %v", err)
}
expected_json := "[\"first line\\n\"]"
if expected_json != string(json) {
t.Errorf("Converted Document: %v did not match %v", string(json), expected_json)
}
}
func TestCanConvertFromRequestToInt(t *testing.T) {
num_lines, err := convertRequestToInt("5\n")
if err != nil {
t.Errorf("Could not convert string to int: %v", err)
}
if 5 != num_lines {
t.Errorf("Could not convert string to 5")
}
}
func TestCanServeDocument(t *testing.T) {
library := Library{}
library.AddDirectory("fixtures")
network_interface := ":9876"
go Listen(&library, network_interface)
conn, err := net.Dial("tcp", network_interface)
if err != nil {
t.Errorf("Error connecting to %v: %v\n", network_interface, err)
}
_, err = conn.Write([]byte("2\n"))
if err !=nil {
t.Errorf("Error writing to %v: %v", network_interface, err)
}
reader := bufio.NewReader(conn)
json_bytes, err := reader.ReadString('\n')
if err != nil {
t.Errorf("Error while reading from server. json_bytes= %v, err= %v",json_bytes,err)
}
lines := make([]string,2)
err = json.Unmarshal([]byte(json_bytes), &lines)
if err != nil {
t.Errorf("Error Decoding JSON: %v, %v",json_bytes, err)
}
if 2 != len(lines) {
t.Errorf("Length of Returned Array was %v not 2",len(lines))
}
}