forked from 0xERR0R/blocky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_test.go
91 lines (85 loc) · 2.42 KB
/
query_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
package cmd
import (
"encoding/json"
"net/http"
"net/http/httptest"
"github.com/0xERR0R/blocky/api"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Blocking command", func() {
var (
ts *httptest.Server
mockFn func(w http.ResponseWriter, _ *http.Request)
)
JustBeforeEach(func() {
ts = testHTTPAPIServer(mockFn)
})
JustAfterEach(func() {
ts.Close()
})
BeforeEach(func() {
mockFn = func(w http.ResponseWriter, _ *http.Request) {}
})
Describe("Call query command", func() {
BeforeEach(func() {
mockFn = func(w http.ResponseWriter, _ *http.Request) {
response, _ := json.Marshal(api.QueryResult{
Reason: "Reason",
ResponseType: "Type",
Response: "Response",
ReturnCode: "NOERROR",
})
_, err := w.Write(response)
Expect(err).Should(Succeed())
}
})
When("query command is called via REST", func() {
BeforeEach(func() {
mockFn = func(w http.ResponseWriter, _ *http.Request) {
response, _ := json.Marshal(api.QueryResult{
Reason: "Reason",
ResponseType: "Type",
Response: "Response",
ReturnCode: "NOERROR",
})
_, err := w.Write(response)
Expect(err).Should(Succeed())
}
})
It("should print result", func() {
query(NewQueryCommand(), []string{"google.de"})
Expect(loggerHook.LastEntry().Message).Should(ContainSubstring("NOERROR"))
})
})
When("Server returns 500", func() {
BeforeEach(func() {
mockFn = func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}
})
It("should end with error", func() {
query(NewQueryCommand(), []string{"google.de"})
Expect(fatal).Should(BeTrue())
Expect(loggerHook.LastEntry().Message).Should(ContainSubstring("NOK: 500 Internal Server Error"))
})
})
When("Type is wrong", func() {
It("should end with error", func() {
command := NewQueryCommand()
command.SetArgs([]string{"--type", "X", "google.de"})
_ = command.Execute()
Expect(fatal).Should(BeTrue())
Expect(loggerHook.LastEntry().Message).Should(ContainSubstring("unknown query type 'X'"))
})
})
When("Url is wrong", func() {
It("should end with error", func() {
apiPort = 0
query(NewQueryCommand(), []string{"google.de"})
Expect(fatal).Should(BeTrue())
Expect(loggerHook.LastEntry().Message).Should(ContainSubstring("connection refused"))
})
})
})
})