Beispiel #1
0
func validateURIHandlerMap(handlers map[string]http.Handler, t *testing.T) {
	assert.Equal(t, 2, len(handlers))
	assert.NotNil(t, handlers["/foo"])
	assert.NotNil(t, handlers["/bar"])
	assert.Nil(t, handlers["no way, Jose"])

	handler := handlers["/foo"]
	handler = timing.NewTimingWrapper().Wrap(handler)

	ts := httptest.NewServer(handler)
	t.Log("test server url", ts.URL)
	defer ts.Close()

	client := &http.Client{}
	req, err := http.NewRequest("GET", ts.URL+"/foo", nil)
	req.Header.Set("Foo", "bar")
	resp, err := client.Do(req)
	assert.Nil(t, err)
	assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode)

	req.Header.Set("Foo", "no match")
	resp, err = client.Do(req)
	assert.Nil(t, err)
	assert.Equal(t, http.StatusNotFound, resp.StatusCode)

	req.Header.Set("Foo", "no match")
	resp, err = client.Do(req)
	assert.Nil(t, err)
	assert.Equal(t, http.StatusNotFound, resp.StatusCode)

}
func TestMRConfigListener(t *testing.T) {
	log.SetLevel(log.InfoLevel)

	var bHandler plugin.MultiBackendHandlerFunc = func(m plugin.BackendHandlerMap, w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(bHandlerStuff))

		ah := m[backendA]
		ar := httptest.NewRecorder()
		ah.ServeHTTP(ar, r)
		assert.Equal(t, aBackendResponse, ar.Body.String())

		bh := m[backendB]
		br := httptest.NewRecorder()
		bh.ServeHTTP(br, r)
		assert.Equal(t, bBackendResponse, br.Body.String())
	}

	var BMBAFactory = func(bhMap plugin.BackendHandlerMap) *plugin.MultiBackendAdapter {
		return &plugin.MultiBackendAdapter{
			BackendHandlerCtx: bhMap,
			Handler:           bHandler,
		}
	}

	plugin.RegisterMultiBackendAdapterFactory(multiBackendAdapterFactory, BMBAFactory)

	AServer := httptest.NewServer(http.HandlerFunc(handleAStuff))
	BServer := httptest.NewServer(http.HandlerFunc(handleBStuff))

	defer AServer.Close()
	defer BServer.Close()

	ms := mrtBuildListener(AServer.URL, BServer.URL)

	uriToRoutesMap := ms.organizeRoutesByUri()
	uriToGuardAndHandlerMap := mapRoutesToGuardAndHandler(uriToRoutesMap)
	uriHandlerMap := makeURIHandlerMap(uriToGuardAndHandlerMap)

	assert.Equal(t, 1, len(uriHandlerMap))

	ls := httptest.NewServer(timing.NewTimingWrapper().Wrap(uriHandlerMap[fooURI]))
	defer ls.Close()

	resp, err := http.Get(ls.URL + fooURI)
	assert.Nil(t, err)
	body, _ := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()
	assert.True(t, strings.Contains(string(body), bHandlerStuff))
}
Beispiel #3
0
func TestPostRequestWithPlugin(t *testing.T) {
	t.Log("Given a server that echos back the post body")
	ts := httptest.NewServer(http.HandlerFunc(postHandler))
	defer ts.Close()

	backend := makeTestBackend(t, ts.URL, "round-robin")

	requestHandler := &requestHandler{
		Transport: &http.Transport{DisableKeepAlives: false, DisableCompression: false},
		Backend:   backend,
	}

	handlerFn := requestHandler.toHandlerFunc()

	wrapper := makeTestWrapper()
	wrappedHandler := (wrapper.Wrap(http.HandlerFunc(handlerFn)))
	wrappedHandler = timing.NewTimingWrapper().Wrap(wrappedHandler)

	t.Log("When the echo server is proxied with a wrapped handler")

	ts2 := httptest.NewServer(wrappedHandler)
	defer ts2.Close()

	payload := `
	{
	"field1","val1",
	"field2","field2"
	}
	`

	req, _ := http.NewRequest("POST", ts2.URL+"/foo", bytes.NewBuffer([]byte(payload)))

	client := &http.Client{}
	resp, err := client.Do(req)
	assert.Nil(t, err)

	defer resp.Body.Close()

	t.Log("Then the response is the request body that was posted as transformed by the handler")
	assert.Equal(t, 200, resp.StatusCode)
	body, _ := ioutil.ReadAll(resp.Body)
	assert.Equal(t, strings.ToUpper(payload), string(body))

}
Beispiel #4
0
func TestGuardFnGenWithBrokerHeaderProp(t *testing.T) {
	ms := makeListenerWithBrokenMsgPropForTest(t)
	uriRoutesMap := ms.organizeRoutesByUri()
	uriToGuardAndHandlerMap := mapRoutesToGuardAndHandler(uriRoutesMap)
	uriHandlerMap := makeURIHandlerMap(uriToGuardAndHandlerMap)

	handler := uriHandlerMap["/foo"]
	handler = timing.NewTimingWrapper().Wrap(handler)

	ts := httptest.NewServer(handler)
	t.Log("test server url", ts.URL)
	defer ts.Close()

	client := &http.Client{}
	req, err := http.NewRequest("GET", ts.URL+"/foo", nil)
	req.Header.Set("Foo", "bar")
	resp, err := client.Do(req)
	assert.Nil(t, err)
	assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)

}