@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 01:00 UTC

master
go 140 lines 4.01 KB
Raw
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 caddyhttp
16
17 import (
18 "context"
19 "io"
20 "net/http"
21 "net/http/httptest"
22 "strconv"
23 "testing"
24
25 "github.com/caddyserver/caddy/v2"
26 )
27
28 func TestStaticResponseHandler(t *testing.T) {
29 r := fakeRequest()
30 w := httptest.NewRecorder()
31
32 s := StaticResponse{
33 StatusCode: WeakString(strconv.Itoa(http.StatusNotFound)),
34 Headers: http.Header{
35 "X-Test": []string{"Testing"},
36 },
37 Body: "Text",
38 Close: true,
39 }
40
41 err := s.ServeHTTP(w, r, nil)
42 if err != nil {
43 t.Errorf("did not expect an error, but got: %v", err)
44 }
45
46 resp := w.Result()
47 respBody, _ := io.ReadAll(resp.Body)
48
49 if resp.StatusCode != http.StatusNotFound {
50 t.Errorf("expected status %d but got %d", http.StatusNotFound, resp.StatusCode)
51 }
52 if resp.Header.Get("X-Test") != "Testing" {
53 t.Errorf("expected x-test header to be 'testing' but was '%s'", resp.Header.Get("X-Test"))
54 }
55 if string(respBody) != "Text" {
56 t.Errorf("expected body to be 'test' but was '%s'", respBody)
57 }
58 }
59
60 func fakeRequest() *http.Request {
61 r, _ := http.NewRequest("GET", "/", nil)
62 repl := caddy.NewReplacer()
63 ctx := context.WithValue(r.Context(), caddy.ReplacerCtxKey, repl)
64 r = r.WithContext(ctx)
65 return r
66 }
67
68 func TestStaticResponseHeadersWithAbsentRequestData(t *testing.T) {
69 req := httptest.NewRequest(http.MethodGet, "/", nil)
70 req = req.WithContext(context.WithValue(req.Context(), VarsCtxKey, map[string]any{}))
71 NewTestReplacer(req)
72 response := StaticResponse{Headers: http.Header{
73 "Location": []string{"/login?session={http.request.cookie.session}"},
74 "X-Tls": []string{"before-{http.request.tls.server_name}-after"},
75 "X-Unknown": []string{"before-{unknown}-after"},
76 }}
77 w := httptest.NewRecorder()
78 if err := response.ServeHTTP(w, req, nil); err != nil {
79 t.Fatal(err)
80 }
81 for field, want := range map[string]string{
82 "Location": "/login?session=",
83 "X-Tls": "before--after",
84 "X-Unknown": "before-{unknown}-after",
85 } {
86 if got := w.Header().Get(field); got != want {
87 t.Errorf("%s = %q, want %q", field, got, want)
88 }
89 }
90 }
91
92 func TestStaticResponseHeadersKeepUnknownPlaceholders(t *testing.T) {
93 r := fakeRequest()
94 w := httptest.NewRecorder()
95
96 s := StaticResponse{
97 StatusCode: WeakString(strconv.Itoa(http.StatusOK)),
98 Headers: http.Header{
99 "X-Json": []string{`{"key":"value"}`},
100 "X-Lit": []string{"value-{not-a-real-placeholder}-kept"},
101 },
102 }
103
104 err := s.ServeHTTP(w, r, nil)
105 if err != nil {
106 t.Errorf("did not expect an error, but got: %v", err)
107 }
108
109 resp := w.Result()
110
111 if got, want := resp.Header.Get("X-Json"), `{"key":"value"}`; got != want {
112 t.Errorf("X-Json header = %q, want %q (unknown placeholders in header values must not be blanked)", got, want)
113 }
114 if got, want := resp.Header.Get("X-Lit"), "value-{not-a-real-placeholder}-kept"; got != want {
115 t.Errorf("X-Lit header = %q, want %q", got, want)
116 }
117 }
118
119 func TestStaticResponseHeadersStillReplaceKnownPlaceholders(t *testing.T) {
120 r := fakeRequest()
121 repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
122 repl.Set("testvar", "replaced")
123 w := httptest.NewRecorder()
124
125 s := StaticResponse{
126 StatusCode: WeakString(strconv.Itoa(http.StatusOK)),
127 Headers: http.Header{
128 "X-Var": []string{"value-{testvar}-end"},
129 },
130 }
131
132 err := s.ServeHTTP(w, r, nil)
133 if err != nil {
134 t.Errorf("did not expect an error, but got: %v", err)
135 }
136
137 if got, want := w.Result().Header.Get("X-Var"), "value-replaced-end"; got != want {
138 t.Errorf("X-Var header = %q, want %q (real placeholders must still expand)", got, want)
139 }
140 }