-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_test.go
117 lines (99 loc) · 3.1 KB
/
proxy_test.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
package main_test
import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
dsp "github.com/cailloumajor/docker-socket-proxy"
"github.com/cailloumajor/docker-socket-proxy/internal/testutils"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
)
type logWriter struct {
tb testing.TB
}
func (w *logWriter) Write(p []byte) (int, error) {
w.tb.Helper()
w.tb.Log(strings.TrimSpace(string(p)))
return len(p), nil
}
func newTestLogger(tb testing.TB) log.Logger {
return level.NewFilter(log.NewLogfmtLogger(&logWriter{tb}), level.AllowError())
}
type testSocketHandler []http.Request
func (h *testSocketHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
*h = append(*h, *req.Clone(context.Background()))
rw.Header().Set("Test-Response", "socket-response")
fmt.Fprintln(rw, "socket response")
}
func TestNewProxyError(t *testing.T) {
sd := t.TempDir()
sf := filepath.Join(sd, "test.sock")
_, err := dsp.NewProxy(sf, log.NewNopLogger())
if msg := testutils.AssertError(t, err, true); msg != "" {
t.Error(msg)
}
}
func TestProxyServeHTTP(t *testing.T) {
sd := t.TempDir()
sf := filepath.Join(sd, "test.sock")
sl, err := net.Listen("unix", sf)
if err != nil {
t.Fatal("error creating socket for testing")
}
var sh testSocketHandler
ss := &httptest.Server{
Listener: sl,
Config: &http.Server{
Handler: &sh,
},
}
ss.Start()
defer ss.Close()
p, err := dsp.NewProxy(sf, newTestLogger(t))
if err != nil {
t.Fatalf("unexpected NewProxy error: %v", err)
}
req := httptest.NewRequest("GET", "/some/endpoint", nil)
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
// Assertions about the response obtained by the client.
if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Errorf("response status: want %d, got %d", want, got)
}
if got, want := resp.Header.Get("Test-Response"), "socket-response"; got != want {
t.Errorf("response `Test-Response` header: want %q, got %q", want, got)
}
if got, want := string(body), "socket response\n"; got != want {
t.Errorf("response body: want %q, got %q", want, got)
}
// Assertions about the request obtained by the socket.
if got, want := len(sh), 1; got != want {
t.Errorf("socket requests count, want %d, got %d", want, got)
}
if got, want := sh[0].Method, http.MethodGet; got != want {
t.Errorf("socket request method: want %q, got %q", want, got)
}
if got, want := sh[0].URL.Scheme, ""; got != want {
t.Errorf("socket request URL scheme: want %q, got %q", want, got)
}
if got, want := sh[0].URL.Host, ""; got != want {
t.Errorf("socket request URL host: want %q, got %q", want, got)
}
if got, want := sh[0].URL.Path, "/some/endpoint"; got != want {
t.Errorf("socket request URL path: want %q, got %q", want, got)
}
if got, want := sh[0].Header.Get("User-Agent"), "docker-socket-proxy/dev"; got != want {
t.Errorf("socket request `User-Agent` header: want %q, got %q", want, got)
}
if got, want := sh[0].Host, "docker-socket"; got != want {
t.Errorf("socket request host: want %q, got %q", want, got)
}
}