File: server_test.go

package info (click to toggle)
golang-github-go-kit-kit 0.13.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,784 kB
  • sloc: sh: 22; makefile: 11
file content (158 lines) | stat: -rw-r--r-- 4,339 bytes parent folder | download | duplicates (5)
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
package httprp_test

import (
	"context"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"

	httptransport "github.com/go-kit/kit/transport/httprp"
)

func TestServerHappyPathSingleServer(t *testing.T) {
	originServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("hey"))
	}))
	defer originServer.Close()
	originURL, _ := url.Parse(originServer.URL)

	handler := httptransport.NewServer(
		originURL,
	)
	proxyServer := httptest.NewServer(handler)
	defer proxyServer.Close()

	resp, _ := http.Get(proxyServer.URL)
	if want, have := http.StatusOK, resp.StatusCode; want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	responseBody, _ := ioutil.ReadAll(resp.Body)
	if want, have := "hey", string(responseBody); want != have {
		t.Errorf("want %q, have %q", want, have)
	}
}

func TestServerHappyPathSingleServerWithServerOptions(t *testing.T) {
	const (
		headerKey = "X-TEST-HEADER"
		headerVal = "go-kit-proxy"
	)

	originServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if want, have := headerVal, r.Header.Get(headerKey); want != have {
			t.Errorf("want %q, have %q", want, have)
		}

		w.WriteHeader(http.StatusOK)
		w.Write([]byte("hey"))
	}))
	defer originServer.Close()
	originURL, _ := url.Parse(originServer.URL)

	handler := httptransport.NewServer(
		originURL,
		httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
			r.Header.Add(headerKey, headerVal)
			return ctx
		}),
	)
	proxyServer := httptest.NewServer(handler)
	defer proxyServer.Close()

	resp, _ := http.Get(proxyServer.URL)
	if want, have := http.StatusOK, resp.StatusCode; want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	responseBody, _ := ioutil.ReadAll(resp.Body)
	if want, have := "hey", string(responseBody); want != have {
		t.Errorf("want %q, have %q", want, have)
	}
}

func TestServerOriginServerNotFoundResponse(t *testing.T) {
	originServer := httptest.NewServer(http.NotFoundHandler())
	defer originServer.Close()
	originURL, _ := url.Parse(originServer.URL)

	handler := httptransport.NewServer(
		originURL,
	)
	proxyServer := httptest.NewServer(handler)
	defer proxyServer.Close()

	resp, _ := http.Get(proxyServer.URL)
	if want, have := http.StatusNotFound, resp.StatusCode; want != have {
		t.Errorf("want %d, have %d", want, have)
	}
}

func TestServerOriginServerUnreachable(t *testing.T) {
	// create a server, then promptly shut it down
	originServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
	}))
	originURL, _ := url.Parse(originServer.URL)
	originServer.Close()

	handler := httptransport.NewServer(
		originURL,
	)
	proxyServer := httptest.NewServer(handler)
	defer proxyServer.Close()

	resp, _ := http.Get(proxyServer.URL)
	switch resp.StatusCode {
	case http.StatusBadGateway: // go1.7 and beyond
		break
	case http.StatusInternalServerError: // to go1.7
		break
	default:
		t.Errorf("want %d or %d, have %d", http.StatusBadGateway, http.StatusInternalServerError, resp.StatusCode)
	}
}

func TestMultipleServerBefore(t *testing.T) {
	const (
		headerKey = "X-TEST-HEADER"
		headerVal = "go-kit-proxy"
	)

	originServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if want, have := headerVal, r.Header.Get(headerKey); want != have {
			t.Errorf("want %q, have %q", want, have)
		}

		w.WriteHeader(http.StatusOK)
		w.Write([]byte("hey"))
	}))
	defer originServer.Close()
	originURL, _ := url.Parse(originServer.URL)

	handler := httptransport.NewServer(
		originURL,
		httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
			r.Header.Add(headerKey, headerVal)
			return ctx
		}),
		httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
			return ctx
		}),
	)
	proxyServer := httptest.NewServer(handler)
	defer proxyServer.Close()

	resp, _ := http.Get(proxyServer.URL)
	if want, have := http.StatusOK, resp.StatusCode; want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	responseBody, _ := ioutil.ReadAll(resp.Body)
	if want, have := "hey", string(responseBody); want != have {
		t.Errorf("want %q, have %q", want, have)
	}
}