From 7751f4fb3b8bc23c947af9b887d507c01e4cee59 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 5 Jun 2016 23:36:04 +0900 Subject: [PATCH] Add API integration tests with http/1.1 and SPDY --- integration-tests/nghttpx_http1_test.go | 112 ++++++++++++++++++++++++ integration-tests/nghttpx_spdy_test.go | 112 ++++++++++++++++++++++++ integration-tests/server_tester.go | 4 + 3 files changed, 228 insertions(+) diff --git a/integration-tests/nghttpx_http1_test.go b/integration-tests/nghttpx_http1_test.go index 199eadcb..9ee25643 100644 --- a/integration-tests/nghttpx_http1_test.go +++ b/integration-tests/nghttpx_http1_test.go @@ -3,6 +3,7 @@ package nghttp2 import ( "bufio" "bytes" + "encoding/json" "fmt" "golang.org/x/net/http2/hpack" "golang.org/x/net/websocket" @@ -793,3 +794,114 @@ func TestH1H2RespPhaseReturn(t *testing.T) { t.Errorf("body = %v; want %v", got, want) } } + +// TestH1APIBackendReplace exercise backend/replace API endpoint +// routine for successful case. +func TestH1APIBackendReplace(t *testing.T) { + st := newServerTesterConnectPort([]string{"-f127.0.0.1,3010;api;no-tls"}, t, func(w http.ResponseWriter, r *http.Request) { + t.Fatalf("request should not be forwarded") + }, 3010) + defer st.Close() + + res, err := st.http1(requestParam{ + name: "TestH1APIBackendReplace", + path: "/api/v1beta1/backend/replace", + method: "PUT", + body: []byte(`# comment +backend=127.0.0.1,3011 + +`), + }) + if err != nil { + t.Fatalf("Error st.http1() = %v", err) + } + if got, want := res.status, 200; got != want { + t.Errorf("res.status: %v; want %v", got, want) + } + + var apiResp APIResponse + err = json.Unmarshal(res.body, &apiResp) + if err != nil { + t.Fatalf("Error unmarshaling API response: %v", err) + } + if got, want := apiResp.Status, "Success"; got != want { + t.Errorf("apiResp.Status: %v; want %v", got, want) + } + if got, want := apiResp.Code, 200; got != want { + t.Errorf("apiResp.Status: %v; want %v", got, want) + } +} + +// TestH1APIBackendReplaceBadMethod exercise backend/replace API +// endpoint routine with bad method. +func TestH1APIBackendReplaceBadMethod(t *testing.T) { + st := newServerTesterConnectPort([]string{"-f127.0.0.1,3010;api;no-tls"}, t, func(w http.ResponseWriter, r *http.Request) { + t.Fatalf("request should not be forwarded") + }, 3010) + defer st.Close() + + res, err := st.http1(requestParam{ + name: "TestH1APIBackendReplaceBadMethod", + path: "/api/v1beta1/backend/replace", + method: "GET", + body: []byte(`# comment +backend=127.0.0.1,3011 + +`), + }) + if err != nil { + t.Fatalf("Error st.http1() = %v", err) + } + if got, want := res.status, 405; got != want { + t.Errorf("res.status: %v; want %v", got, want) + } + + var apiResp APIResponse + err = json.Unmarshal(res.body, &apiResp) + if err != nil { + t.Fatalf("Error unmarshaling API response: %v", err) + } + if got, want := apiResp.Status, "Failure"; got != want { + t.Errorf("apiResp.Status: %v; want %v", got, want) + } + if got, want := apiResp.Code, 405; got != want { + t.Errorf("apiResp.Status: %v; want %v", got, want) + } +} + +// TestH1APINotFound exercise backend/replace API endpoint routine +// when API endpoint is not found. +func TestH1APINotFound(t *testing.T) { + st := newServerTesterConnectPort([]string{"-f127.0.0.1,3010;api;no-tls"}, t, func(w http.ResponseWriter, r *http.Request) { + t.Fatalf("request should not be forwarded") + }, 3010) + defer st.Close() + + res, err := st.http1(requestParam{ + name: "TestH1APINotFound", + path: "/api/notfound", + method: "GET", + body: []byte(`# comment +backend=127.0.0.1,3011 + +`), + }) + if err != nil { + t.Fatalf("Error st.http1() = %v", err) + } + if got, want := res.status, 404; got != want { + t.Errorf("res.status: %v; want %v", got, want) + } + + var apiResp APIResponse + err = json.Unmarshal(res.body, &apiResp) + if err != nil { + t.Fatalf("Error unmarshaling API response: %v", err) + } + if got, want := apiResp.Status, "Failure"; got != want { + t.Errorf("apiResp.Status: %v; want %v", got, want) + } + if got, want := apiResp.Code, 404; got != want { + t.Errorf("apiResp.Status: %v; want %v", got, want) + } +} diff --git a/integration-tests/nghttpx_spdy_test.go b/integration-tests/nghttpx_spdy_test.go index e6723af0..ecab829f 100644 --- a/integration-tests/nghttpx_spdy_test.go +++ b/integration-tests/nghttpx_spdy_test.go @@ -1,6 +1,7 @@ package nghttp2 import ( + "encoding/json" "github.com/tatsuhiro-t/spdy" "golang.org/x/net/http2/hpack" "net/http" @@ -474,3 +475,114 @@ func TestS3H2RespPhaseReturn(t *testing.T) { t.Errorf("body = %v; want %v", got, want) } } + +// TestS3APIBackendReplace exercise backend/replace API endpoint +// routine for successful case. +func TestS3APIBackendReplace(t *testing.T) { + st := newServerTesterTLSConnectPort([]string{"--npn-list=spdy/3.1", "-f127.0.0.1,3010;api"}, t, func(w http.ResponseWriter, r *http.Request) { + t.Fatalf("request should not be forwarded") + }, 3010) + defer st.Close() + + res, err := st.spdy(requestParam{ + name: "TestS3APIBackendReplace", + path: "/api/v1beta1/backend/replace", + method: "PUT", + body: []byte(`# comment +backend=127.0.0.1,3011 + +`), + }) + if err != nil { + t.Fatalf("Error st.spdy() = %v", err) + } + if got, want := res.status, 200; got != want { + t.Errorf("res.status: %v; want %v", got, want) + } + + var apiResp APIResponse + err = json.Unmarshal(res.body, &apiResp) + if err != nil { + t.Fatalf("Error unmarshaling API response: %v", err) + } + if got, want := apiResp.Status, "Success"; got != want { + t.Errorf("apiResp.Status: %v; want %v", got, want) + } + if got, want := apiResp.Code, 200; got != want { + t.Errorf("apiResp.Status: %v; want %v", got, want) + } +} + +// TestS3APIBackendReplaceBadMethod exercise backend/replace API +// endpoint routine with bad method. +func TestS3APIBackendReplaceBadMethod(t *testing.T) { + st := newServerTesterTLSConnectPort([]string{"--npn-list=spdy/3.1", "-f127.0.0.1,3010;api"}, t, func(w http.ResponseWriter, r *http.Request) { + t.Fatalf("request should not be forwarded") + }, 3010) + defer st.Close() + + res, err := st.spdy(requestParam{ + name: "TestS3APIBackendReplaceBadMethod", + path: "/api/v1beta1/backend/replace", + method: "GET", + body: []byte(`# comment +backend=127.0.0.1,3011 + +`), + }) + if err != nil { + t.Fatalf("Error st.spdy() = %v", err) + } + if got, want := res.status, 405; got != want { + t.Errorf("res.status: %v; want %v", got, want) + } + + var apiResp APIResponse + err = json.Unmarshal(res.body, &apiResp) + if err != nil { + t.Fatalf("Error unmarshaling API response: %v", err) + } + if got, want := apiResp.Status, "Failure"; got != want { + t.Errorf("apiResp.Status: %v; want %v", got, want) + } + if got, want := apiResp.Code, 405; got != want { + t.Errorf("apiResp.Status: %v; want %v", got, want) + } +} + +// TestS3APINotFound exercise backend/replace API endpoint routine +// when API endpoint is not found. +func TestS3APINotFound(t *testing.T) { + st := newServerTesterTLSConnectPort([]string{"--npn-list=spdy/3.1", "-f127.0.0.1,3010;api"}, t, func(w http.ResponseWriter, r *http.Request) { + t.Fatalf("request should not be forwarded") + }, 3010) + defer st.Close() + + res, err := st.spdy(requestParam{ + name: "TestS3APINotFound", + path: "/api/notfound", + method: "GET", + body: []byte(`# comment +backend=127.0.0.1,3011 + +`), + }) + if err != nil { + t.Fatalf("Error st.spdy() = %v", err) + } + if got, want := res.status, 404; got != want { + t.Errorf("res.status: %v; want %v", got, want) + } + + var apiResp APIResponse + err = json.Unmarshal(res.body, &apiResp) + if err != nil { + t.Fatalf("Error unmarshaling API response: %v", err) + } + if got, want := apiResp.Status, "Failure"; got != want { + t.Errorf("apiResp.Status: %v; want %v", got, want) + } + if got, want := apiResp.Code, 404; got != want { + t.Errorf("apiResp.Status: %v; want %v", got, want) + } +} diff --git a/integration-tests/server_tester.go b/integration-tests/server_tester.go index 7111a094..50859cbb 100644 --- a/integration-tests/server_tester.go +++ b/integration-tests/server_tester.go @@ -82,6 +82,10 @@ func newServerTesterTLS(args []string, t *testing.T, handler http.HandlerFunc) * return newServerTesterInternal(args, t, handler, true, serverPort, nil) } +func newServerTesterTLSConnectPort(args []string, t *testing.T, handler http.HandlerFunc, port int) *serverTester { + return newServerTesterInternal(args, t, handler, true, port, nil) +} + // newServerTester creates test context for TLS frontend connection // with given clientConfig func newServerTesterTLSConfig(args []string, t *testing.T, handler http.HandlerFunc, clientConfig *tls.Config) *serverTester {