-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspaceapi_test.go
137 lines (119 loc) · 2.96 KB
/
spaceapi_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
package spaceapi
import "fmt"
import "strings"
import "testing"
import "encoding/json"
import "net/http"
import "net/http/httptest"
var minimal_spaceapi string = `
{
"api": "0.13",
"space": "Slopspace",
"logo": "http://your-space.org/img/logo.png",
"url": "http://your-space.org",
"location": {
"address": "Ulmer Strasse 255, 70327 Stuttgart, Germany",
"lon": 9.236,
"lat": 48.777
},
"icon": {
"open": "http://test.org/a.png",
"closed": "http://test.org/b.png"
},
"contact": {
"twitter": "@spaceapi"
},
"issue_report_channels": [
"twitter"
],
"state": {
"open": false
},
"sensors": {
"temperature": {
"value": "23",
"unit": "°C",
"location": "inside"
}
},
"feeds": {
"blog": {
"type": "rss",
"url": "http://www.goatse.cx"
}
},
"projects": [
"http://www.weltraumpflege.org"
]
}`
func TestSpaceAPIUnmarshal(t *testing.T) {
txt := []byte(minimal_spaceapi)
var e SpaceAPI
err := json.Unmarshal(txt, &e)
if err != nil {
t.Error(err)
}
if e.Space != "Slopspace" {
t.Error("Error parsing main Object")
}
if e.Location.Address != "Ulmer Strasse 255, 70327 Stuttgart, Germany" {
t.Error("Error parsing Location Object")
}
if e.State.Open != False {
t.Error("Error parsing State Object")
}
if e.Contact.Twitter != "@spaceapi" {
t.Error("Error parsing Contact Object")
}
if len(e.IssueReportChannels) < 1 {
t.Error("Error parsing IssueReportChannels")
}
if len(e.Feeds) < 1 {
t.Error("Error parsing Feeds")
}
if len(e.Projects) < 1 {
t.Error("Error parsing Projects")
}
}
func TestSpaceApiGetEndpoint(t *testing.T) {
test_server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, minimal_spaceapi)
}))
defer test_server.Close()
test_endpoint := Endpoint{endpoint_url: test_server.URL}
test_struct, err := test_endpoint.GetSpaceAPIData()
if err != nil {
t.Error(err)
}
if test_struct.Space != "Slopspace" {
t.Error("Error parsing JSON over HTTP")
}
if len(test_struct.Projects) < 1 {
t.Error("Error parsing JSON over HTTP")
}
}
func TestSpaceAPIMarshaling(t *testing.T) {
location := Location{Lat: 10.8867969, Lon: 48.3577121}
state := State{Open: True}
contact := Contact{Email: "[email protected]"}
api_data := NewSpaceAPI("0.14", "OpenLab", "http://www.goatse.info", "http://www.openlab-augsburg.de", &location, &state, &contact, []string{"email"})
json_str, err := api_data.ToJSON()
if err != nil {
t.Error(err)
}
if !strings.Contains(json_str, "OpenLab") {
t.Error(err)
}
if !strings.Contains(json_str, "10.8867") {
t.Error(err)
}
if !strings.Contains(json_str, "kontakt@") {
t.Error(err)
}
if strings.Contains(json_str, "projects") {
t.Error(err)
}
if strings.Contains(json_str, "spacefed") {
t.Error(api_data.SpaceFed)
}
}