| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ |
| 16 | |
| 17 | import ( |
| 18 | "contextscip-go gomod github.com/golang/go/src go1.25.1 context/" |
| 19 | "ioscip-go gomod github.com/golang/go/src go1.25.1 io/" |
| 20 | "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/" |
| 21 | "net/http/httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/" |
| 22 | "strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/" |
| 23 | "testingscip-go gomod github.com/golang/go/src go1.25.1 testing/" |
| 24 | |
| 25 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 26 | ) |
| 27 | |
| 28 | func TestStaticResponseHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/TestStaticResponseHandler().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 29 | r := fakeRequestscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/fakeRequest().() |
| 30 | w := httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/.NewRecorderscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/NewRecorder().() |
| 31 | |
| 32 | s := StaticResponsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/StaticResponse#{ |
| 33 | StatusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/StaticResponse#StatusCode.: WeakStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#(strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/.Itoascip-go gomod github.com/golang/go/src go1.25.1 strconv/Itoa().(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusNotFoundscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusNotFound.)), |
| 34 | Headersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/StaticResponse#Headers.: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#{ |
| 35 | "X-Test": []string{"Testing"}, |
| 36 | }, |
| 37 | Bodyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/StaticResponse#Body.: "Text", |
| 38 | Closescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/StaticResponse#Close.: true, |
| 39 | } |
| 40 | |
| 41 | err := s.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/StaticResponse#ServeHTTP().(w, r, nil) |
| 42 | if err != nil { |
| 43 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("did not expect an error, but got: %v", err) |
| 44 | } |
| 45 | |
| 46 | resp := w.Resultscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/ResponseRecorder#Result().() |
| 47 | respBody, _ := ioscip-go gomod github.com/golang/go/src go1.25.1 io/.ReadAllscip-go gomod github.com/golang/go/src go1.25.1 io/ReadAll().(resp.Bodyscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Response#Body.) |
| 48 | |
| 49 | if resp.StatusCodescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Response#StatusCode. != httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusNotFoundscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusNotFound. { |
| 50 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("expected status %d but got %d", httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusNotFoundscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusNotFound., resp.StatusCodescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Response#StatusCode.) |
| 51 | } |
| 52 | if resp.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Response#Header..Getscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Get().("X-Test") != "Testing" { |
| 53 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("expected x-test header to be 'testing' but was '%s'", resp.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Response#Header..Getscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Get().("X-Test")) |
| 54 | } |
| 55 | if string(respBody) != "Text" { |
| 56 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("expected body to be 'test' but was '%s'", respBody) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func fakeRequestscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/fakeRequest().() *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request# { |
| 61 | r, _ := httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.NewRequestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/NewRequest().("GET", "/", nil) |
| 62 | repl := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewReplacer().() |
| 63 | ctx := contextscip-go gomod github.com/golang/go/src go1.25.1 context/.WithValuescip-go gomod github.com/golang/go/src go1.25.1 context/WithValue().(r.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().(), caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ReplacerCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ReplacerCtxKey., repl) |
| 64 | r = r.WithContextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#WithContext().(ctx) |
| 65 | return r |
| 66 | } |