|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + nethttp "net/http" |
| 8 | + "net/url" |
| 9 | + "time" |
| 10 | + |
| 11 | + contractshttp "github.com/goravel/framework/contracts/http" |
| 12 | + contractsession "github.com/goravel/framework/contracts/session" |
| 13 | +) |
| 14 | + |
| 15 | +type TestContext struct { |
| 16 | + ctx context.Context |
| 17 | + next nethttp.Handler |
| 18 | + request *nethttp.Request |
| 19 | + writer nethttp.ResponseWriter |
| 20 | +} |
| 21 | + |
| 22 | +func NewTestContext(ctx context.Context, next nethttp.Handler, w nethttp.ResponseWriter, r *nethttp.Request) *TestContext { |
| 23 | + return &TestContext{ |
| 24 | + ctx: ctx, |
| 25 | + next: next, |
| 26 | + request: r, |
| 27 | + writer: w, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func (r *TestContext) Deadline() (deadline time.Time, ok bool) { |
| 32 | + panic("do not need to implement it") |
| 33 | +} |
| 34 | + |
| 35 | +func (r *TestContext) Done() <-chan struct{} { |
| 36 | + panic("do not need to implement it") |
| 37 | +} |
| 38 | + |
| 39 | +func (r *TestContext) Err() error { |
| 40 | + panic("do not need to implement it") |
| 41 | +} |
| 42 | + |
| 43 | +func (r *TestContext) Value(key any) any { |
| 44 | + return r.ctx.Value(key) |
| 45 | +} |
| 46 | + |
| 47 | +func (r *TestContext) Context() context.Context { |
| 48 | + return r.ctx |
| 49 | +} |
| 50 | + |
| 51 | +func (r *TestContext) WithContext(context.Context) { |
| 52 | + panic("do not need to implement it") |
| 53 | +} |
| 54 | + |
| 55 | +func (r *TestContext) WithValue(key any, value any) { |
| 56 | + r.ctx = context.WithValue(r.ctx, key, value) |
| 57 | +} |
| 58 | + |
| 59 | +func (r *TestContext) Request() contractshttp.ContextRequest { |
| 60 | + return NewTestRequest(r) |
| 61 | +} |
| 62 | + |
| 63 | +func (r *TestContext) Response() contractshttp.ContextResponse { |
| 64 | + return NewTestResponse(r) |
| 65 | +} |
| 66 | + |
| 67 | +type TestRequest struct { |
| 68 | + contractshttp.ContextRequest |
| 69 | + ctx *TestContext |
| 70 | +} |
| 71 | + |
| 72 | +func NewTestRequest(ctx *TestContext) *TestRequest { |
| 73 | + return &TestRequest{ |
| 74 | + ctx: ctx, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func (r *TestRequest) Path() string { |
| 79 | + if r.ctx != nil && r.ctx.request != nil { |
| 80 | + return r.ctx.request.URL.Path |
| 81 | + } |
| 82 | + return "" |
| 83 | +} |
| 84 | + |
| 85 | +func (r *TestRequest) Ip() string { |
| 86 | + return "127.0.0.1" |
| 87 | +} |
| 88 | + |
| 89 | +func (r *TestRequest) Cookie(key string, defaultValue ...string) string { |
| 90 | + cookie, err := r.ctx.request.Cookie(key) |
| 91 | + if err != nil { |
| 92 | + if len(defaultValue) > 0 { |
| 93 | + return defaultValue[0] |
| 94 | + } |
| 95 | + |
| 96 | + return "" |
| 97 | + } |
| 98 | + |
| 99 | + val, _ := url.QueryUnescape(cookie.Value) |
| 100 | + return val |
| 101 | +} |
| 102 | + |
| 103 | +func (r *TestRequest) HasSession() bool { |
| 104 | + if r.ctx == nil { |
| 105 | + return false |
| 106 | + } |
| 107 | + session := r.Session() |
| 108 | + return session != nil |
| 109 | +} |
| 110 | + |
| 111 | +func (r *TestRequest) Session() contractsession.Session { |
| 112 | + s, ok := r.ctx.Value("session").(contractsession.Session) |
| 113 | + if !ok { |
| 114 | + return nil |
| 115 | + } |
| 116 | + return s |
| 117 | +} |
| 118 | + |
| 119 | +func (r *TestRequest) SetSession(session contractsession.Session) contractshttp.ContextRequest { |
| 120 | + r.ctx.WithValue("session", session) |
| 121 | + r.ctx.request = r.ctx.request.WithContext(r.ctx.Context()) |
| 122 | + return r |
| 123 | +} |
| 124 | + |
| 125 | +func (r *TestRequest) Abort(code ...int) { |
| 126 | + r.ctx.writer.WriteHeader(code[0]) |
| 127 | +} |
| 128 | + |
| 129 | +func (r *TestRequest) Next() { |
| 130 | + if r.ctx != nil && r.ctx.next != nil { |
| 131 | + r.ctx.next.ServeHTTP(r.ctx.writer, r.ctx.request.WithContext(r.ctx.Context())) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func (r *TestRequest) Method() string { |
| 136 | + return r.ctx.request.Method |
| 137 | +} |
| 138 | + |
| 139 | +func (r *TestRequest) Header(key string, defaultValue ...string) string { |
| 140 | + headerValue := r.ctx.request.Header.Get(key) |
| 141 | + if len(headerValue) > 0 { |
| 142 | + return headerValue |
| 143 | + } else if len(defaultValue) > 0 { |
| 144 | + return defaultValue[0] |
| 145 | + } |
| 146 | + return "" |
| 147 | +} |
| 148 | + |
| 149 | +func (r *TestRequest) Input(key string, defualtVaue ...string) string { |
| 150 | + if body, err := io.ReadAll(r.ctx.request.Body); err != nil { |
| 151 | + if len(defualtVaue) > 0 { |
| 152 | + return defualtVaue[0] |
| 153 | + } |
| 154 | + return "" |
| 155 | + } else { |
| 156 | + data := map[string]any{} |
| 157 | + if err := json.Unmarshal(body, &data); err != nil { |
| 158 | + return "" |
| 159 | + } |
| 160 | + if data[key] == nil { |
| 161 | + return "" |
| 162 | + } |
| 163 | + return data[key].(string) |
| 164 | + } |
| 165 | + |
| 166 | +} |
| 167 | + |
| 168 | +type TestResponse struct { |
| 169 | + contractshttp.ContextResponse |
| 170 | + ctx *TestContext |
| 171 | +} |
| 172 | + |
| 173 | +func NewTestResponse(ctx *TestContext) *TestResponse { |
| 174 | + return &TestResponse{ |
| 175 | + ctx: ctx, |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +func (r *TestResponse) Cookie(cookie contractshttp.Cookie) contractshttp.ContextResponse { |
| 180 | + path := cookie.Path |
| 181 | + if path == "" { |
| 182 | + path = "/" |
| 183 | + } |
| 184 | + nethttp.SetCookie(r.ctx.writer, &nethttp.Cookie{ |
| 185 | + Name: cookie.Name, |
| 186 | + Value: url.QueryEscape(cookie.Value), |
| 187 | + MaxAge: cookie.MaxAge, |
| 188 | + Path: path, |
| 189 | + Domain: cookie.Domain, |
| 190 | + Secure: cookie.Secure, |
| 191 | + HttpOnly: cookie.HttpOnly, |
| 192 | + }) |
| 193 | + |
| 194 | + return r |
| 195 | +} |
| 196 | + |
| 197 | +func (r *TestResponse) Header(key string, value string) contractshttp.ContextResponse { |
| 198 | + r.ctx.writer.Header().Set(key, value) |
| 199 | + return r |
| 200 | +} |
0 commit comments