func TestCoProcessMiddleware(t *testing.T) {
	spec := MakeCoProcessSampleAPI(basicCoProcessDef)

	chain := BuildCoProcessChain(spec, "hook_test", coprocess.HookType_Pre, tykcommon.MiddlewareDriver("python"))

	thisSession := createNonThrottledSession()
	spec.SessionManager.UpdateSession("abc", thisSession, 60)

	uri := "/headers"
	method := "GET"

	recorder := httptest.NewRecorder()

	param := make(url.Values)

	req, err := http.NewRequest(method, uri, bytes.NewBufferString(param.Encode()))
	req.Header.Add("authorization", "abc")

	if err != nil {
		t.Fatal(err)
	}

	chain.ServeHTTP(recorder, req)

}
func TestCoProcessAuth(t *testing.T) {
	t.Log("CP AUTH")
	spec := MakeCoProcessSampleAPI(protectedCoProcessDef)

	chain := BuildCoProcessChain(spec, "hook_test_bad_auth", coprocess.HookType_CustomKeyCheck, tykcommon.MiddlewareDriver("python"))

	thisSession := createNonThrottledSession()
	spec.SessionManager.UpdateSession("abc", thisSession, 60)

	uri := "/headers"
	method := "GET"

	recorder := httptest.NewRecorder()

	param := make(url.Values)

	req, err := http.NewRequest(method, uri, bytes.NewBufferString(param.Encode()))
	req.Header.Add("authorization", "abc")

	if err != nil {
		t.Fatal(err)
	}

	chain.ServeHTTP(recorder, req)

	if recorder.Code != 403 {
		t.Fatal("Authentication should fail! But it's returning:", recorder.Code)
	}
}
func TestCoProcessObjectPostProcess(t *testing.T) {
	spec := MakeCoProcessSampleAPI(basicCoProcessDef)

	chain := BuildCoProcessChain(spec, "hook_test_object_postprocess", coprocess.HookType_Pre, tykcommon.MiddlewareDriver("python"))

	thisSession := createNonThrottledSession()
	spec.SessionManager.UpdateSession("abc", thisSession, 60)

	uri := "/headers"
	method := "GET"

	recorder := httptest.NewRecorder()

	param := make(url.Values)

	req, err := http.NewRequest(method, uri, bytes.NewBufferString(param.Encode()))
	req.Header.Add("authorization", "abc")
	req.Header.Add("Deletethisheader", "value")

	if err != nil {
		t.Fatal(err)
	}

	chain.ServeHTTP(recorder, req)

	headersResponse := HttpbinHeadersResponse{}
	err = json.Unmarshal(recorder.Body.Bytes(), &headersResponse)

	if err != nil {
		t.Fatal(err)
	}

	if headersResponse.Headers["Test"] != "value" {
		t.Fatal("ObjectPostProcess couldn't add a header.")
	}

	if headersResponse.Headers["Deletethisheader"] != "" {
		t.Fatal("ObjectPostProcess couldn't delete a header.")
	}

	recorder = httptest.NewRecorder()

	uri = "/get?a=a_value&b=123&remove=3"
	getReq, err := http.NewRequest(method, uri, bytes.NewBufferString(param.Encode()))
	getReq.Header.Add("authorization", "abc")

	if err != nil {
		t.Fatal(err)
	}

	chain.ServeHTTP(recorder, getReq)

	getResponse := HttpbinGetResponse{}
	err = json.Unmarshal(recorder.Body.Bytes(), &getResponse)

	if err != nil {
		t.Fatal(err)
	}

	if getResponse.Params["a"] != "a_value" || getResponse.Params["b"] != "123" {
		t.Fatal("The original parameters don't match.")
	}

	if getResponse.Params["remove"] != "" {
		t.Fatal("ObjectPostProcess couldn't remove a parameter.")
	}

	if getResponse.Params["customparam"] != "customvalue" {
		t.Fatal("ObjectPostProcess couldn't set custom parameters.")
	}

}