This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
94 lines (74 loc) · 2.52 KB
/
api_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
package apidCounters
import (
"encoding/json"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)
// BeforeSuite setup and AfterSuite cleanup is in apidCounters_suite_test.go
var _ = Describe("api", func() {
It("should get a counter", func() {
counter := "api_test_1"
count, err := getDBCounter(counter)
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(0))
uri, err := url.Parse(testServer.URL)
Expect(err).NotTo(HaveOccurred())
uri.Path = fmt.Sprintf(countersBasePath+"/%s", counter)
resp, err := http.Get(uri.String())
Expect(err).ShouldNot(HaveOccurred())
defer resp.Body.Close()
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
body, _ := ioutil.ReadAll(resp.Body)
Expect(string(body)).Should(Equal(strconv.Itoa(count)))
})
It("should increment a counter", func() {
counter := "api_test_2"
count, err := getDBCounter(counter)
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(0))
uri, err := url.Parse(testServer.URL)
Expect(err).NotTo(HaveOccurred())
uri.Path = fmt.Sprintf(countersBasePath+"/%s", counter)
req, err := http.NewRequest("POST", uri.String(), nil)
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
Expect(err).ShouldNot(HaveOccurred())
defer resp.Body.Close()
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
body, err := ioutil.ReadAll(resp.Body)
Expect(string(body)).Should(Equal(strconv.Itoa(count)))
time.Sleep(50 * time.Millisecond) // todo: hack - make this a listen event on incr event finished
count, err = getDBCounter(counter)
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(1))
})
It("should retrieve a list of counters", func() {
counter1 := "api_test_3"
counter2 := "api_test_4"
err := incrementDBCounter(counter1)
Expect(err).NotTo(HaveOccurred())
err = incrementDBCounter(counter2)
Expect(err).NotTo(HaveOccurred())
err = incrementDBCounter(counter2)
Expect(err).NotTo(HaveOccurred())
uri, err := url.Parse(testServer.URL)
Expect(err).NotTo(HaveOccurred())
uri.Path = countersBasePath
resp, err := http.Get(uri.String())
Expect(err).ShouldNot(HaveOccurred())
defer resp.Body.Close()
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
body, err := ioutil.ReadAll(resp.Body)
Expect(err).ShouldNot(HaveOccurred())
var counterMap map[string]int
json.Unmarshal(body, &counterMap)
Expect(counterMap[counter1]).Should(Equal(1))
Expect(counterMap[counter2]).Should(Equal(2))
})
})