summaryrefslogtreecommitdiff
path: root/caddytest/integration/reverseproxy_test.go
blob: 0505becf72ddb0d30c81a47713c28ad7cf7887c2 (plain)
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
195
196
197
198
199
200
package integration

import (
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"os"
	"runtime"
	"strings"
	"testing"

	"github.com/caddyserver/caddy/v2/caddytest"
)

func TestSRVReverseProxy(t *testing.T) {
	tester := caddytest.NewTester(t)
	tester.InitServer(`
	{
		"apps": {
		  "http": {
			"servers": {
			  "srv0": {
				"listen": [
				  ":8080"
				],
				"routes": [
				  {
					"handle": [
					  {
						"handler": "reverse_proxy",
						"upstreams": [
						  {
							"lookup_srv": "srv.host.service.consul"
						  }
						]
					  }
					]
				  }
				]
			  }
			}
		  }
		}
	  }
  `, "json")
}

func TestSRVWithDial(t *testing.T) {
	caddytest.AssertLoadError(t, `
	{
		"apps": {
		  "http": {
			"servers": {
			  "srv0": {
				"listen": [
				  ":8080"
				],
				"routes": [
				  {
					"handle": [
					  {
						"handler": "reverse_proxy",
						"upstreams": [
						  {
							"dial": "tcp/address.to.upstream:80",
							"lookup_srv": "srv.host.service.consul"
						  }
						]
					  }
					]
				  }
				]
			  }
			}
		  }
		}
	  }
	`, "json", `upstream: specifying dial address is incompatible with lookup_srv: 0: {\"dial\": \"tcp/address.to.upstream:80\", \"lookup_srv\": \"srv.host.service.consul\"}`)
}

func TestSRVWithActiveHealthcheck(t *testing.T) {
	caddytest.AssertLoadError(t, `
	{
		"apps": {
		  "http": {
			"servers": {
			  "srv0": {
				"listen": [
				  ":8080"
				],
				"routes": [
				  {
					"handle": [
					  {
						"handler": "reverse_proxy",
						"health_checks": {
							"active": {
								"path": "/ok"
							}
						},
						"upstreams": [
						  {
							"lookup_srv": "srv.host.service.consul"
						  }
						]
					  }
					]
				  }
				]
			  }
			}
		  }
		}
	  }
	`, "json", `upstream: lookup_srv is incompatible with active health checks: 0: {\"dial\": \"\", \"lookup_srv\": \"srv.host.service.consul\"}`)
}

func TestReverseProxyHealthCheck(t *testing.T) {
	tester := caddytest.NewTester(t)
	tester.InitServer(`
	{
		http_port     9080
		https_port    9443
	}
	http://localhost:2020 {
		respond "Hello, World!"
	}
	http://localhost:2021 {
		respond "ok"
	}
	http://localhost:9080 {
		reverse_proxy {
			to localhost:2020
	
			health_path /health
			health_port 2021
			health_interval 2s
			health_timeout 5s
		}
	}
  `, "caddyfile")

	tester.AssertGetResponse("http://localhost:9080/", 200, "Hello, World!")
}

func TestReverseProxyHealthCheckUnixSocket(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.SkipNow()
	}
	tester := caddytest.NewTester(t)
	f, err := ioutil.TempFile("", "*.sock")
	if err != nil {
		t.Errorf("failed to create TempFile: %s", err)
		return
	}
	// a hack to get a file name within a valid path to use as socket
	socketName := f.Name()
	os.Remove(f.Name())

	server := http.Server{
		Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
			if strings.HasPrefix(req.URL.Path, "/health") {
				w.Write([]byte("ok"))
				return
			}
			w.Write([]byte("Hello, World!"))
		}),
	}

	unixListener, err := net.Listen("unix", socketName)
	if err != nil {
		t.Errorf("failed to listen on the socket: %s", err)
		return
	}
	go server.Serve(unixListener)
	t.Cleanup(func() {
		server.Close()
	})
	runtime.Gosched() // Allow other goroutines to run

	tester.InitServer(fmt.Sprintf(`
	{
		http_port     9080
		https_port    9443
	}
	http://localhost:9080 {
		reverse_proxy {
			to unix/%s
	
			health_path /health
			health_port 2021
			health_interval 2s
			health_timeout 5s
		}
	}
	`, socketName), "caddyfile")

	tester.AssertGetResponse("http://localhost:9080/", 200, "Hello, World!")
}