-
Notifications
You must be signed in to change notification settings - Fork 2
/
bogus_examples_test.go
102 lines (79 loc) · 2.33 KB
/
bogus_examples_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
package bogus_test
import (
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/gomicro/bogus"
"github.com/franela/goblin"
. "github.com/onsi/gomega"
)
func ExampleBogus() {
// This would normally be provided by a normal testing function setup
var t *testing.T
server := bogus.New()
server.AddPath("/foo/bar").
SetMethods("GET").
SetPayload([]byte("some return payload")).
SetStatus(http.StatusOK)
host, port := server.HostPort()
resp, err := http.Get(fmt.Sprintf("https://%v:%v", host, port))
if err != nil {
t.Errorf("expected nil error, got %v", err.Error())
}
defer resp.Body.Close()
if server.Hits() != 1 {
t.Errorf("expected server to be hit once, got %v", server.Hits())
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("expected nil error, got: %v", err.Error())
}
if string(bodyBytes) != "some return payload" {
t.Errorf("Expected a different payload, got %v", string(bodyBytes))
}
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status 200, got %v", resp.StatusCode)
}
}
func ExampleBogus_goblinGomega() {
// This would normally be provided by a normal testing function setup
var t *testing.T
g := goblin.Goblin(t)
RegisterFailHandler(func(m string, _ ...int) { g.Fail(m) })
g.Describe("Tests needing a test server", func() {
var server *bogus.Bogus
g.BeforeEach(func() {
server = bogus.New()
})
g.It("should connect to a test server", func() {
server.AddPath("/").
SetMethods("GET")
host, port := server.HostPort()
_, err := http.Get(fmt.Sprintf("https://%v:%v", host, port))
Expect(err).To(BeNil())
Expect(server.Hits()).To(Equal(1))
})
})
g.Describe("Tests needing a test server", func() {
var server *bogus.Bogus
g.BeforeEach(func() {
server = bogus.New()
})
g.It("should connect to a test server", func() {
server.AddPath("/foo/bar").
SetMethods("GET").
SetPayload([]byte("some return payload")).
SetStatus(http.StatusOK)
host, port := server.HostPort()
resp, err := http.Get(fmt.Sprintf("https://%v:%v", host, port))
Expect(err).To(BeNil())
defer resp.Body.Close()
Expect(server.Hits()).To(Equal(1))
bodyBytes, err := ioutil.ReadAll(resp.Body)
Expect(err).To(BeNil())
Expect(string(bodyBytes)).To(Equal("some return payload"))
Expect(resp.StatusCode).To(Equal(http.StatusOK))
})
})
}