コード例 #1
0
ファイル: headers_test.go プロジェクト: na--/nedomi
func TestVia(t *testing.T) {
	t.Parallel()
	canonicalKey := http.CanonicalHeaderKey("via")
	var testText = "notnedomi 2.2"
	v, err := New(config.NewHandler("headers", json.RawMessage(`{
		"response": {
			"add_headers": {
				"vIa": "notnedomi 2.2"
			}
		}
	}`)), nil, handlerCode(200))

	if err != nil {
		t.Errorf("Got error when initializing via - %s", err)
	}
	var expect []string
	recorder := httptest.NewRecorder()
	req, err := http.NewRequest("get", "/to/test", nil)
	if err != nil {
		t.Fatal(err)
	}
	v.RequestHandle(nil, recorder, req)
	expect = []string{testText}
	testHeaders(t, canonicalKey, expect, recorder.Header())

	recorder.Header().Set(canonicalKey, "holla")
	expect = []string{"holla", testText}
	v.RequestHandle(nil, recorder, req)
	testHeaders(t, canonicalKey, expect, recorder.Header())
}
コード例 #2
0
ファイル: via_test.go プロジェクト: na--/nedomi
func TestVia(t *testing.T) {
	t.Parallel()
	canonicalKey := http.CanonicalHeaderKey("via")
	var testText = "notnedomi 2.2"
	v, err := New(config.NewHandler("via", json.RawMessage(`{"text": "notnedomi 2.2"}`)), nil,
		testStringHandler(t, testText))

	if err != nil {
		t.Errorf("Got error when initializing via - %s", err)
	}
	var expect, got []string
	recorder := httptest.NewRecorder()
	v.RequestHandle(nil, recorder, nil)
	expect = []string{testText}
	got = recorder.Header()[canonicalKey]
	if !reflect.DeepEqual(got, expect) {
		t.Errorf("expected via header to be equal to %s but got %s", expect, got)
	}

	recorder.Header().Set(canonicalKey, "holla")
	expect = []string{"holla", testText}
	v.RequestHandle(nil, recorder, nil)
	got = recorder.Header()[canonicalKey]
	if !reflect.DeepEqual(got, expect) {
		t.Errorf("expected via header to be equal to %s but got %s", expect, got)
	}
}
コード例 #3
0
ファイル: flv_test.go プロジェクト: na--/nedomi
func setup(t *testing.T) types.RequestHandler {
	var v, err = New(config.NewHandler("flv", json.RawMessage(``)), nil, fsMapHandler())
	if err != nil {
		t.Fatalf("error on creating new flv handler - %s", err)
	}

	return v
}
コード例 #4
0
ファイル: new_test.go プロジェクト: na--/nedomi
func TestErrorOnNonExistingHandler(t *testing.T) {
	t.Parallel()
	_, err := New(config.NewHandler("bogus_handler", nil), nil, nil)

	if err == nil {
		t.Error("No error returned with bogus handler.")
	}
}
コード例 #5
0
ファイル: headers_test.go プロジェクト: na--/nedomi
func TestWithBrokenConfig(t *testing.T) {
	t.Parallel()
	v, err := New(config.NewHandler("headers", json.RawMessage(`{
		"response: {
			"add_headers": {
				"vIa": "notnedomi 2.2"
			}
		}
	}`)), nil, handlerCode(200))
	if err == nil {
		t.Errorf("Expected error on initializing with broken config but got %+v", v)
	}
}
コード例 #6
0
ファイル: headers_test.go プロジェクト: na--/nedomi
func TestNilNext(t *testing.T) {
	t.Parallel()
	v, err := New(config.NewHandler("headers", json.RawMessage(`{
		"response": {
			"add_headers": {
				"vIa": "notnedomi 2.2"
			}
		}
	}`)), nil, nil)
	if err == nil {
		t.Errorf("Expected error on initializing with nil next handler but got %+v", v)
	}
}
コード例 #7
0
ファイル: headers_test.go プロジェクト: na--/nedomi
func TestRemoveFromRequest(t *testing.T) {
	t.Parallel()
	var expectedHeaders = map[string][]string{
		"Via":     nil,
		"Added":   {"old value", "added value"},
		"Setting": {"this", "header"},
	}
	v, err := New(config.NewHandler("headers", json.RawMessage(`{
		"request": {
			"remove_headers": ["vIa"],
			"add_headers":  {
				"added": "added value"
			},
			"set_headers": {
				"setting": ["this", "header"]
			}
		}
	}`)), nil, types.RequestHandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		for key, expected := range expectedHeaders {
			got := r.Header[http.CanonicalHeaderKey(key)]
			if !reflect.DeepEqual(got, expected) {
				t.Errorf("for header '%s' expected '%+v', got '%+v'", key, got, expected)
			}
		}
	}))

	if err != nil {
		t.Errorf("Got error when initializing via - %s", err)
	}
	recorder := httptest.NewRecorder()
	req, err := http.NewRequest("get", "/to/test", nil)
	if err != nil {
		t.Fatal(err)
	}
	req.Header.Add(http.CanonicalHeaderKey("via"), "this should be removed")
	req.Header.Add(http.CanonicalHeaderKey("added"), "old value")
	req.Header.Add(http.CanonicalHeaderKey("setting"), "this should be resetted")
	v.RequestHandle(nil, recorder, req)
}
コード例 #8
0
ファイル: headers_test.go プロジェクト: na--/nedomi
func TestRemoveFromResponse(t *testing.T) {
	t.Parallel()
	canonicalKey := http.CanonicalHeaderKey("via")
	v, err := New(config.NewHandler("headers", json.RawMessage(`{
		"response": {
			"remove_headers": ["vIa"]
		}
	}`)), nil, addHeaderHandler(t, "via", "pesho"))

	if err != nil {
		t.Errorf("Got error when initializing via - %s", err)
	}
	recorder := httptest.NewRecorder()
	req, err := http.NewRequest("get", "/to/test", nil)
	if err != nil {
		t.Fatal(err)
	}
	v.RequestHandle(nil, recorder, req)
	var got = recorder.Header()[canonicalKey]
	if len(got) != 0 {
		t.Errorf("expected via header to be removed but got %s", got)
	}
}
コード例 #9
0
ファイル: headers_test.go プロジェクト: na--/nedomi
func TestCodes(t *testing.T) {
	t.Parallel()
	var codes = []int{http.StatusPartialContent, http.StatusOK, http.StatusNotFound, http.StatusTeapot}
	for _, code := range codes {
		canonicalKey := http.CanonicalHeaderKey("via")
		var testText = "notnedomi 2.2"
		v, err := New(config.NewHandler("headers", json.RawMessage(`{
		"response": {
			"add_headers": {
				"vIa": "notnedomi 2.2"
			}
		},
		"request": {
			"add_headers": {
				"via": "notnedomi 2.2"
			}
		}
	}`)), nil, handlerCode(code))

		if err != nil {
			t.Errorf("Got error when initializing via - %s", err)
		}
		var expect []string
		rec := httptest.NewRecorder()
		req, err := http.NewRequest("get", "/to/test", nil)
		if err != nil {
			t.Fatal(err)
		}
		v.RequestHandle(nil, rec, req)
		expect = []string{testText}
		testHeaders(t, canonicalKey, expect, rec.Header())
		if code != rec.Code {
			t.Errorf("expected code %d got %d", code, rec.Code)
		}
	}
}
コード例 #10
0
func TestSimpleRetryWithNilUpstream(t *testing.T) {
	t.Parallel()
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/err" {
			w.WriteHeader(404)
			fmt.Fprint(w, "error!")
			return
		}
		w.WriteHeader(200)
		fmt.Fprint(w, "hello world")
	}))
	defer ts.Close()

	upstreamURL, err := url.Parse(ts.URL)
	if err != nil {
		t.Fatal(err)
	}
	up, err := upstream.NewSimple(upstreamURL)
	if err != nil {
		t.Fatal(err)
	}

	retryTs := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/err" {
			w.WriteHeader(200)
			fmt.Fprint(w, "not error!")
			return
		}
		w.WriteHeader(404)
		fmt.Fprint(w, "not hello world")
	}))
	defer retryTs.Close()

	retryUpstreamURL, err := url.Parse(retryTs.URL)
	if err != nil {
		t.Fatal(err)
	}
	retryUpstream, err := upstream.NewSimple(retryUpstreamURL)
	if err != nil {
		t.Fatal(err)
	}

	proxy, err := New(
		config.NewHandler("proxy", json.RawMessage(`{ "try_other_upstream_on_code" : {"404": "nonexistant_upstream"}}`)),
		&types.Location{
			Name:     "test",
			Logger:   mock.NewLogger(),
			Upstream: up,
		}, nil)
	if err != nil {
		t.Fatal(err)
	}

	req1, err := http.NewRequest("GET", "http://www.somewhere.com/err", nil)
	if err != nil {
		t.Fatal(err)
	}
	resp1 := httptest.NewRecorder()
	ctx := contexts.NewAppContext(context.Background(), &mockApp{
		upstreams: map[string]types.Upstream{
			"retry_upstream": retryUpstream,
		},
	})
	proxy.ServeHTTP(ctx, resp1, req1)
	if resp1.Code != 404 || resp1.Body.String() != "error!" {
		t.Errorf("Unexpected response %#v", resp1)
	}

	req2, err := http.NewRequest("GET", "http://www.somewhere.com/index", nil)
	if err != nil {
		t.Fatal(err)
	}
	resp2 := httptest.NewRecorder()
	proxy.ServeHTTP(nil, resp2, req2)
	if resp2.Code != 200 || resp2.Body.String() != "hello world" {
		t.Errorf("Unexpected response %#v", resp2)
	}
}