-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmockery_test.go
More file actions
125 lines (106 loc) · 4.46 KB
/
mockery_test.go
File metadata and controls
125 lines (106 loc) · 4.46 KB
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
package mockery_test
import (
"log"
"net/http"
"testing"
"github.com/UpCloudLtd/http-mockery/pkg/mockery"
"github.com/stretchr/testify/assert"
)
func testingConfig() mockery.Config {
return mockery.Config{
Endpoints: []mockery.Endpoint{
{
Type: mockery.EndpointTypeNormal,
Uri: "/example",
Method: "GET",
ResponseCode: 200,
Template: "../../test/response-example.json",
Variables: []mockery.Variable{
{
Name: "item1",
Value: "test123",
},
{
Name: "auth_username",
Header: "authorization_username",
},
},
},
{
Type: mockery.EndpointTypeNormal,
Uri: "/resource",
Method: "POST",
ResponseCode: 201,
Template: "../../test/response-resource-creation.json",
Variables: []mockery.Variable{},
},
{
Type: mockery.EndpointTypeRegex,
Uri: "/resource/[0-9]+",
Method: "DELETE",
ResponseCode: 204,
},
},
}
}
func TestConfigLoading(t *testing.T) {
_, err := mockery.OpenConfigFile("test/nonexisting-config.json")
assert.NotEqual(t, nil, err, "opening nonexistent config should cause an error")
assert.ErrorContains(t, err, "no such file or directory", "error should be about file not existing")
conf, err := mockery.OpenConfigFile("../../test/working-min-config.json")
assert.Equal(t, nil, err, "working config should be opened without issues")
assert.Equal(t, "1.2.3.4", conf.ListenIP, "IP should be set from config file")
assert.Equal(t, 1337, conf.ListenPort, "Port should be set from config file")
assert.Equal(t, 0, len(conf.Endpoints), "Minimal config should not have any configured endpoints")
_, err = mockery.OpenConfigFile("../../test/broken-config.json")
assert.NotEqual(t, nil, err, "opening broken config should cause an error")
assert.ErrorContains(t, err, "invalid character", "error should be about invalid characters")
}
func TestConfigValidation(t *testing.T) {
testMocker := mockery.MockHandler{
Config: testingConfig(),
Log: log.Default(),
}
err := testMocker.ValidateConfig()
assert.Equal(t, nil, err, "unmodified test config should have no errors")
testMocker.Config.Endpoints[0].Method = "TRACE"
err = testMocker.ValidateConfig()
assert.ErrorContains(t, err, "invalid HTTP method", "Config validation should fail on unsupported HTTP method")
testMocker.Config = testingConfig()
testMocker.Config.Endpoints[0].ResponseCode = 0
err = testMocker.ValidateConfig()
assert.ErrorContains(t, err, "must include response_code", "Config validation should fail on undefined response code")
}
func TestTemplateRendering(t *testing.T) {
const username = "user12345"
testMocker := mockery.MockHandler{
Config: testingConfig(),
Log: log.Default(),
}
rendered, err := testMocker.RenderTemplateResponse(testMocker.Config.Endpoints[0], basicAuthRequest(username, "bar"))
assert.Equal(t, nil, err, "Template should be rendered correctly")
assert.Contains(t, rendered, testMocker.Config.Endpoints[0].Variables[0].Value, "Template should contain rendered value from config")
assert.Contains(t, rendered, username, "Template should contain rendered value from config")
assert.True(t, mockery.IsJSON(rendered), "Rendered template should be valid JSON")
}
func TestEndpointMatching(t *testing.T) {
testMocker := mockery.MockHandler{
Config: testingConfig(),
Log: log.Default(),
}
_, err := testMocker.MatchEndpoint(&http.Request{Method: http.MethodGet, RequestURI: "/example2"})
assert.EqualError(t, err, mockery.ErrEndpointNotFound.Error(), "Should not match when URI is different")
_, err = testMocker.MatchEndpoint(&http.Request{Method: http.MethodDelete, RequestURI: "/example"})
assert.EqualError(t, err, mockery.ErrEndpointNotFound.Error(), "Should not match when HTTP method is different")
endpoint, err := testMocker.MatchEndpoint(&http.Request{Method: http.MethodPost, RequestURI: "/resource"})
assert.Equal(t, nil, err, "Endpoint should match")
assert.Equal(t, testMocker.Config.Endpoints[1], endpoint, "Should be correctly matched endpoint")
endpoint, err = testMocker.MatchEndpoint(&http.Request{Method: http.MethodDelete, RequestURI: "/resource/14354"})
assert.Equal(t, nil, err, "Endpoint should match")
assert.Equal(t, testMocker.Config.Endpoints[2], endpoint, "Should be correctly matched endpoint")
}
func basicAuthRequest(username, password string) *http.Request {
r := http.Request{Header: http.Header{}}
r.SetBasicAuth(username, password)
return &r
}