-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash_briefing_test.go
174 lines (147 loc) · 4.32 KB
/
flash_briefing_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
package alexa_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
"github.com/tyndyll/alexa"
)
const (
demoUID = "urn:uuid:1335c695-cfb8-4ebb-abbd-80da344efa6b"
demoDate = "2016-05-23T22:34:51Z"
demoTitle = "Amazon Developer Blog, week in review May 23rd"
demoText = "Main information for the item"
demoStream = "https://developer.amazon.com/public/community/blog/myaudiofile.mp3"
demoRedirection = "https://developer.amazon.com/public/community/blog"
demoJSON = `{"uid":"%s","updateDate":"%s","titleText":"%s","mainText":"%s","streamUrl":"%s","redirectionUrl":"%s"}`
max_items = 5
)
var expectedJSON = fmt.Sprintf(demoJSON, demoUID, demoDate, demoTitle, demoText, demoStream, demoRedirection)
func TestFlashBriefingItem_MarshalJSON(t *testing.T) {
Convey(`Given I have a FlashBriefingItem`, t, func() {
date, err := time.Parse(time.RFC3339, demoDate)
if err != nil {
panic(err)
}
item := &alexa.FlashBriefingItem{
ID: demoUID,
Date: date,
Title: demoTitle,
Text: demoText,
AudioURL: demoStream,
DisplayURL: demoRedirection,
}
Convey(`When I marshal the item to JSON`, func() {
data, err := json.Marshal(item)
Convey(`Then the error will be nil`, func() {
So(err, ShouldBeNil)
})
Convey(`Then the correct json will be produced`, func() {
So(string(data), ShouldEqual, expectedJSON)
})
})
})
}
func TestFlashBriefing_MarshalJSON(t *testing.T) {
Convey(`Given I have a FlashBriefingItem`, t, func() {
date, err := time.Parse(time.RFC3339, demoDate)
if err != nil {
panic(err)
}
item := &alexa.FlashBriefingItem{
ID: demoUID,
Date: date,
Title: demoTitle,
Text: demoText,
AudioURL: demoStream,
DisplayURL: demoRedirection,
}
Convey(`And I populate a FlashBriefing with that item`, func() {
briefing := &alexa.FlashBriefing{
Items: []*alexa.FlashBriefingItem{
item,
item,
},
}
Convey(`When I marshal the briefing to JSON`, func() {
data, err := json.Marshal(briefing)
Convey(`Then the error will be nil`, func() {
So(err, ShouldBeNil)
})
Convey(`Then the correct json will be produced`, func() {
expectedBriefing := fmt.Sprintf(`[%s,%s]`, expectedJSON, expectedJSON)
So(string(data), ShouldEqual, expectedBriefing)
})
})
})
})
}
func TestServeFlashBriefing(t *testing.T) {
Convey(`Given I have a FlashBriefingItem`, t, func() {
date, err := time.Parse(time.RFC3339, demoDate)
if err != nil {
panic(err)
}
item := &alexa.FlashBriefingItem{
ID: demoUID,
Date: date,
Title: demoTitle,
Text: demoText,
AudioURL: demoStream,
DisplayURL: demoRedirection,
}
Convey(`And I populate a FlashBriefing with that item`, func() {
briefing := &alexa.FlashBriefing{
Items: []*alexa.FlashBriefingItem{
item,
item,
},
}
Convey(`And I have a http handler`, func() {
handler := alexa.FlashBriefingHandler(briefing)
Convey(`And I have a HTTP request`, func() {
req := httptest.NewRequest("GET", `/`, nil)
Convey(`When I make the request`, func() {
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
Convey(`Then the response will have the appropriate Content-Type`, func() {
// TODO: Fix Test
// So(w.HeaderMap.Get(contentHeader), ShouldEqual, jsonContentType)
})
Convey(`Then the body will be the FlashBriefing as JSON`, func() {
body, err := ioutil.ReadAll(w.Body)
if err != nil {
panic(err)
}
marshaledJSON, err := json.Marshal(briefing)
if err != nil {
panic(err)
}
So(body, ShouldResemble, marshaledJSON)
})
})
})
})
})
})
}
func ExampleFlashBriefingHandler() {
items := make([]*alexa.FlashBriefingItem, max_items)
briefing := &alexa.FlashBriefing{
Items: items,
}
for i := 0; i < max_items; i++ {
briefing.Items[i] = &alexa.FlashBriefingItem{
ID: fmt.Sprintf(`id-%d`, i),
Date: time.Now(),
Title: fmt.Sprintf(`Demo Entry %d`, i),
Text: fmt.Sprintf(`This is the entry for %d`, i),
}
}
http.HandleFunc(`/`, alexa.FlashBriefingHandler(briefing))
http.ListenAndServe(`:8080`, nil)
}