Skip to content
This repository was archived by the owner on Aug 7, 2022. It is now read-only.

Commit ffc01ab

Browse files
committed
fix: more unit tests
1 parent a3cae37 commit ffc01ab

File tree

6 files changed

+87
-56
lines changed

6 files changed

+87
-56
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import (
1212
"net/http"
1313
"testing"
1414

15-
mock "github.com/mockingio/go"
15+
mock "github.com/mockingio/mock"
1616
)
1717

1818
func Test_Example(t *testing.T) {
19-
srv := mock.
19+
srv, _ := mock.
2020
New().
2121
Get("/hello").
2222
Response(http.StatusOK, "hello world").
23-
Start(t)
23+
Start()
2424
defer srv.Close()
2525

2626
req, _ := http.NewRequest("GET", srv.URL, nil)
@@ -30,12 +30,12 @@ func Test_Example(t *testing.T) {
3030
}
3131

3232
func Test_Example_WithRules(t *testing.T) {
33-
srv := mock.
33+
srv, _ := mock.
3434
New().
3535
Get("/hello").
3636
When("cookie", "name", "equal", "Chocolate").
3737
Response(http.StatusOK, "hello world").
38-
Start(t)
38+
Start()
3939
defer srv.Close()
4040

4141
req, _ := http.NewRequest("GET", srv.URL, nil)

builder.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package _go
1+
package mock
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
67
"net/http/httptest"
7-
"testing"
8-
8+
99
"github.com/google/uuid"
1010

1111
"github.com/mockingio/engine"
@@ -41,10 +41,10 @@ type Builder struct {
4141
config *mock.Mock
4242
}
4343

44-
func (b *Builder) Start(t *testing.T) *httptest.Server {
44+
func (b *Builder) Start() (*httptest.Server, error) {
4545
b.clear()
4646
if err := b.config.Validate(); err != nil {
47-
t.Errorf("invalid config: %v", err)
47+
return nil, fmt.Errorf("invalid config: %v", err)
4848
}
4949
id := uuid.NewString()
5050
b.config.ID = id
@@ -55,7 +55,7 @@ func (b *Builder) Start(t *testing.T) *httptest.Server {
5555

5656
m := engine.New(id, mem)
5757

58-
return httptest.NewServer(http.HandlerFunc(m.Handler))
58+
return httptest.NewServer(http.HandlerFunc(m.Handler)), nil
5959
}
6060

6161
func (b *Builder) Post(url string) *Builder {

builder_test.go

+63-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package _go_test
1+
package mock_test
22

33
import (
44
"fmt"
@@ -7,105 +7,120 @@ import (
77
"net/http/httptest"
88
"strings"
99
"testing"
10+
"time"
1011

1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
1314

14-
. "github.com/mockingio/go"
15+
. "github.com/mockingio/mock"
1516
)
1617

18+
func TestBuilder_Validation(t *testing.T) {
19+
_, err := New().Start()
20+
assert.Error(t, err)
21+
}
22+
1723
func TestBuilder_MatchedRoute(t *testing.T) {
1824
t.Run("simple get", func(t *testing.T) {
19-
srv := New().
25+
srv, err := New().
2026
Get("/hello").
2127
Response(http.StatusOK, "world").
22-
Start(t)
28+
Start()
2329
defer srv.Close()
2430

31+
require.NoError(t, err)
2532
assertHTTPGETRequest(t, url(srv, "/hello"), 200, "world")
2633
})
2734

2835
t.Run("simple post", func(t *testing.T) {
29-
srv := New().
36+
srv, err := New().
3037
Post("/hello").
3138
Response(http.StatusOK, "world").
32-
Start(t)
39+
Start()
3340
defer srv.Close()
3441

42+
require.NoError(t, err)
3543
assertHTTPPOSTRequest(t, url(srv, "/hello"), "", 200, "world")
3644
})
3745

3846
t.Run("simple put", func(t *testing.T) {
39-
srv := New().
47+
srv, err := New().
4048
Put("/hello").
4149
Response(http.StatusOK, "world").
42-
Start(t)
50+
Start()
4351
defer srv.Close()
4452

53+
require.NoError(t, err)
4554
assertHTTPPUTRequest(t, url(srv, "/hello"), "", 200, "world")
4655
})
4756

4857
t.Run("simple options", func(t *testing.T) {
49-
srv := New().
58+
srv, err := New().
5059
Option("/hello").
5160
Response(http.StatusOK, "world").
52-
Start(t)
61+
Start()
5362
defer srv.Close()
5463

64+
require.NoError(t, err)
5565
assertHTTPOPTIONSRequest(t, url(srv, "/hello"), "", 200, "world")
5666
})
5767

5868
t.Run("simple put", func(t *testing.T) {
59-
srv := New().
69+
srv, err := New().
6070
Delete("/hello").
6171
Response(http.StatusOK, "world").
62-
Start(t)
72+
Start()
6373
defer srv.Close()
6474

75+
require.NoError(t, err)
6576
assertHTTPDELETETRequest(t, url(srv, "/hello"), "", 200, "world")
6677
})
6778

6879
t.Run("simple get with builder", func(t *testing.T) {
6980
builder := New()
7081
builder.Get("/hello").
7182
Response(http.StatusOK, "world")
72-
srv := builder.Start(t)
83+
srv, err := builder.Start()
7384
defer srv.Close()
7485

86+
require.NoError(t, err)
7587
assertHTTPGETRequest(t, url(srv, "/hello"), 200, "world")
7688
})
7789

7890
t.Run("with a condition", func(t *testing.T) {
79-
srv := New().
91+
srv, err := New().
8092
Get("/hello").
8193
Response(http.StatusOK, "world").
8294
When(Cookie, "name", Equal, "Jack").
83-
Start(t)
95+
Start()
8496

97+
require.NoError(t, err)
8598
assertHTTPGETRequest(t, url(srv, "/hello"), 200, "world")
8699
})
87100

88101
t.Run("with AND condition", func(t *testing.T) {
89-
srv := New().
102+
srv, err := New().
90103
Get("/hello").
91104
Response(http.StatusOK, "world").
92105
When(Cookie, "name", Equal, "Jack").
93106
And(Cookie, "name", Regex, "[a-zA-Z]+").
94107
And(Header, "x-type", Regex, "x-men").
95-
Start(t)
108+
Start()
96109

110+
require.NoError(t, err)
97111
assertHTTPGETRequest(t, url(srv, "/hello"), 200, "world")
98112
})
99113

100114
t.Run("with OR condition", func(t *testing.T) {
101-
srv := New().
115+
srv, err := New().
102116
Get("/hello").
103117
Response(http.StatusOK, "world").
104118
When(Cookie, "name", Equal, "Jack").
105119
Or(Header, "name", Regex, "non exist value").
106120
Or(Header, "age", Equal, "18").
107-
Start(t)
121+
Start()
108122

123+
require.NoError(t, err)
109124
assertHTTPGETRequest(t, url(srv, "/hello"), 200, "world")
110125
})
111126

@@ -120,56 +135,76 @@ func TestBuilder_MatchedRoute(t *testing.T) {
120135
builder.Post("/hello3").
121136
Response(http.StatusOK, "world3")
122137

123-
srv := builder.Start(t)
138+
srv, err := builder.Start()
124139
defer srv.Close()
125140

141+
require.NoError(t, err)
126142
assertHTTPGETRequest(t, url(srv, "/hello1"), 200, "world1")
127143
assertHTTPGETRequest(t, url(srv, "/hello2"), 200, "world2")
128144
assertHTTPPOSTRequest(t, url(srv, "/hello3"), "", 200, "world3")
129145
})
146+
147+
t.Run("with delay", func(t *testing.T) {
148+
srv, err := New().
149+
Get("/hello").
150+
Response(http.StatusOK, "world").
151+
Delay(50).
152+
Start()
153+
defer srv.Close()
154+
155+
timer := time.Now()
156+
157+
require.NoError(t, err)
158+
assertHTTPGETRequest(t, url(srv, "/hello"), 200, "world")
159+
assert.Greater(t, time.Since(timer), 50*time.Millisecond)
160+
})
130161
}
131162

132163
func TestBuilder_NoMatchedRoute(t *testing.T) {
133164
t.Run("simple get", func(t *testing.T) {
134-
srv := New().
165+
srv, err := New().
135166
Get("/hello").
136167
Response(http.StatusOK, "world").
137-
Start(t)
168+
Start()
138169
defer srv.Close()
139170

171+
require.NoError(t, err)
140172
assertNoMatchHTTPGETRequest(t, url(srv, "/hellox"), 200)
141173
})
142174

143175
t.Run("with a condition", func(t *testing.T) {
144-
srv := New().
176+
srv, err := New().
145177
Get("/hello").
146178
Response(http.StatusOK, "world").
147179
When(Cookie, "name", Equal, "Jack not found").
148-
Start(t)
180+
Start()
149181

182+
require.NoError(t, err)
150183
assertNoMatchHTTPGETRequest(t, url(srv, "/hello"), 200)
151184
})
152185

153186
t.Run("with AND condition", func(t *testing.T) {
154-
srv := New().
187+
srv, err := New().
155188
Get("/hello").
156189
Response(http.StatusOK, "world").
157190
When(Cookie, "name", Equal, "Jack").
158191
And(Cookie, "name", Regex, "[a-zA-Z]+").
159192
And(Header, "x-type", Regex, "x-women").
160-
Start(t)
193+
Start()
161194

195+
require.NoError(t, err)
162196
assertNoMatchHTTPGETRequest(t, url(srv, "/hello"), 200)
163197
})
164198

165199
t.Run("with OR condition", func(t *testing.T) {
166-
srv := New().
200+
srv, err := New().
167201
Get("/hello").
168202
Response(http.StatusOK, "world").
169203
When(Cookie, "name", Equal, "Jack not found").
170204
Or(Header, "name", Regex, "non exist value").
171-
Start(t)
205+
Start()
172206

207+
require.NoError(t, err)
173208
assertNoMatchHTTPGETRequest(t, url(srv, "/hello"), 200)
174209
})
175210
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/mockingio/go
1+
module github.com/mockingio/mock
22

33
go 1.18
44

response.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
package _go
1+
package mock
22

33
import (
4-
"net/http/httptest"
5-
"testing"
6-
74
"github.com/mockingio/engine/mock"
5+
"net/http/httptest"
86
)
97

108
type Response struct {
@@ -16,8 +14,8 @@ func (r *Response) Delay(delay int64) *Response {
1614
return r
1715
}
1816

19-
func (r *Response) Start(t *testing.T) *httptest.Server {
20-
return r.builder.Start(t)
17+
func (r *Response) Start() (*httptest.Server, error) {
18+
return r.builder.Start()
2119
}
2220

2321
func (r *Response) When(target, modifier, operator, value string) *When {
@@ -38,8 +36,8 @@ type And struct {
3836
builder *Builder
3937
}
4038

41-
func (a *And) Start(t *testing.T) *httptest.Server {
42-
return a.builder.Start(t)
39+
func (a *And) Start() (*httptest.Server, error) {
40+
return a.builder.Start()
4341
}
4442

4543
func (a *And) And(target, modifier, operator, value string) *And {
@@ -68,6 +66,6 @@ func (o *Or) Or(target, modifier, operator, value string) *Or {
6866
return o
6967
}
7068

71-
func (o *Or) Start(t *testing.T) *httptest.Server {
72-
return o.builder.Start(t)
69+
func (o *Or) Start() (*httptest.Server, error) {
70+
return o.builder.Start()
7371
}

when.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
package _go
1+
package mock
22

33
import (
4-
"net/http/httptest"
5-
"testing"
6-
74
"github.com/mockingio/engine/mock"
5+
"net/http/httptest"
86
)
97

108
type When struct {
119
builder *Builder
1210
}
1311

14-
func (w *When) Start(t *testing.T) *httptest.Server {
15-
return w.builder.Start(t)
12+
func (w *When) Start() (*httptest.Server, error) {
13+
return w.builder.Start()
1614
}
1715

1816
func (w *When) And(target, modifier, operator, value string) *And {

0 commit comments

Comments
 (0)