-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathmock_context.go
194 lines (163 loc) · 5.05 KB
/
mock_context.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package fuego
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/go-fuego/fuego/internal"
)
// MockContext provides a framework-agnostic implementation of ContextWithBody
// for testing purposes. It allows testing controllers without depending on
// specific web frameworks like Gin or Echo.
type MockContext[B any] struct {
internal.CommonContext[B]
RequestBody B
Headers http.Header
PathParams map[string]string
response http.ResponseWriter
request *http.Request
Cookies map[string]*http.Cookie
}
// NewMockContext creates a new MockContext instance with the provided body
func NewMockContext[B any](body B) *MockContext[B] {
return &MockContext[B]{
CommonContext: internal.CommonContext[B]{
CommonCtx: context.Background(),
UrlValues: make(url.Values),
OpenAPIParams: make(map[string]internal.OpenAPIParam),
DefaultStatusCode: http.StatusOK,
},
RequestBody: body,
Headers: make(http.Header),
PathParams: make(map[string]string),
Cookies: make(map[string]*http.Cookie),
}
}
// NewMockContextNoBody creates a new MockContext suitable for a request & controller with no body
func NewMockContextNoBody() *MockContext[any] {
return NewMockContext[any](nil)
}
var _ ContextWithBody[string] = &MockContext[string]{}
// Body returns the previously set body value
func (m *MockContext[B]) Body() (B, error) {
return m.RequestBody, nil
}
// MustBody returns the body or panics if there's an error
func (m *MockContext[B]) MustBody() B {
return m.RequestBody
}
// HasHeader checks if a header exists
func (m *MockContext[B]) HasHeader(key string) bool {
_, exists := m.Headers[key]
return exists
}
// HasCookie checks if a cookie exists
func (m *MockContext[B]) HasCookie(key string) bool {
_, exists := m.Cookies[key]
return exists
}
// Header returns the value of the specified header
func (m *MockContext[B]) Header(key string) string {
return m.Headers.Get(key)
}
// SetHeader sets a header in the mock context
func (m *MockContext[B]) SetHeader(key, value string) {
m.Headers.Set(key, value)
}
// PathParam returns a mock path parameter
func (m *MockContext[B]) PathParam(name string) string {
return m.PathParams[name]
}
func (m *MockContext[B]) PathParamIntErr(name string) (int, error) {
return strconv.Atoi(m.PathParams[name])
}
func (m *MockContext[B]) PathParamInt(name string) int {
if i, err := m.PathParamIntErr(name); err == nil {
return i
}
return 0
}
// Request returns the mock request
func (m *MockContext[B]) Request() *http.Request {
return m.request
}
// Response returns the mock response writer
func (m *MockContext[B]) Response() http.ResponseWriter {
return m.response
}
// SetStatus sets the response status code
func (m *MockContext[B]) SetStatus(code int) {
if m.response != nil {
m.response.WriteHeader(code)
}
}
// Cookie returns a mock cookie
func (m *MockContext[B]) Cookie(name string) (*http.Cookie, error) {
cookie, exists := m.Cookies[name]
if !exists {
return nil, http.ErrNoCookie
}
return cookie, nil
}
// SetCookie sets a cookie in the mock context
func (m *MockContext[B]) SetCookie(cookie http.Cookie) {
m.Cookies[cookie.Name] = &cookie
}
// MainLang returns the main language from Accept-Language header
func (m *MockContext[B]) MainLang() string {
lang := m.Headers.Get("Accept-Language")
if lang == "" {
return ""
}
return strings.Split(strings.Split(lang, ",")[0], "-")[0]
}
// MainLocale returns the main locale from Accept-Language header
func (m *MockContext[B]) MainLocale() string {
return m.Headers.Get("Accept-Language")
}
// Redirect returns a redirect response
func (m *MockContext[B]) Redirect(code int, url string) (any, error) {
if m.response != nil {
http.Redirect(m.response, m.request, url, code)
}
return nil, nil
}
// Render is a mock implementation that does nothing
func (m *MockContext[B]) Render(templateToExecute string, data any, templateGlobsToOverride ...string) (CtxRenderer, error) {
panic("not implemented")
}
// SetQueryParam adds a query parameter to the mock context with OpenAPI validation
func (m *MockContext[B]) SetQueryParam(name, value string) *MockContext[B] {
param := OpenAPIParam{
Name: name,
GoType: "string",
Type: "query",
}
m.CommonContext.OpenAPIParams[name] = param
m.CommonContext.UrlValues.Set(name, value)
return m
}
// SetQueryParamInt adds an integer query parameter to the mock context with OpenAPI validation
func (m *MockContext[B]) SetQueryParamInt(name string, value int) *MockContext[B] {
param := OpenAPIParam{
Name: name,
GoType: "integer",
Type: "query",
}
m.CommonContext.OpenAPIParams[name] = param
m.CommonContext.UrlValues.Set(name, fmt.Sprintf("%d", value))
return m
}
// SetQueryParamBool adds a boolean query parameter to the mock context with OpenAPI validation
func (m *MockContext[B]) SetQueryParamBool(name string, value bool) *MockContext[B] {
param := OpenAPIParam{
Name: name,
GoType: "boolean",
Type: "query",
}
m.CommonContext.OpenAPIParams[name] = param
m.CommonContext.UrlValues.Set(name, fmt.Sprintf("%t", value))
return m
}