From e653b765b74fb532421ec77cdd0a019434237b31 Mon Sep 17 00:00:00 2001 From: Kilowhisky Date: Mon, 6 May 2024 21:59:32 -0700 Subject: [PATCH] Add tests --- tests/proto_test.go | 52 +++++++++++++++++++++++++++++++++++++++++++++ tests/tests_test.go | 1 + 2 files changed, 53 insertions(+) create mode 100644 tests/proto_test.go diff --git a/tests/proto_test.go b/tests/proto_test.go new file mode 100644 index 00000000..40f87dfe --- /dev/null +++ b/tests/proto_test.go @@ -0,0 +1,52 @@ +package tests + +import ( + "fmt" + "net/http" +) + +func subTestProto(g *testGroup) { + g.regSubTest("HTTP CORS", proto_HTTP_CORS_test) +} + +func proto_HTTP_CORS_test(mc *mockServer) error { + // Make CORS request for GET /SERVER + morigin := "http://my-test-origin" + url := fmt.Sprintf("http://127.0.0.1:%d/SERVER", mc.port) + req, err := http.NewRequest(http.MethodOptions, url, nil) + if err != nil { + return err + } + req.Header.Add("Origin", morigin) + req.Header.Add("Access-Control-Request-Method", "GET") + req.Header.Add("Access-Control-Request-Headers", "Authorization") + resp, err := http.DefaultClient.Do(req) + + // Validate CORS response + if err != nil { + return err + } + if resp.StatusCode != 204 { + return fmt.Errorf("expected http stuats '204', got '%d'", resp.StatusCode) + } + origin := resp.Header.Get("Access-Control-Allow-Origin") + methods := resp.Header.Get("Access-Control-Allow-Methods") + if !(origin == "*" || origin == morigin) { + return fmt.Errorf("expected http access-control-allow-origin value '*', got '%s'", origin) + } + if methods != "POST, GET, OPTIONS" { + return fmt.Errorf("expected http access-control-allow-Methods value 'POST, GET, OPTIONS', got '%s'", methods) + } + + // Make the actual request now + resp, err = http.Get(url) + if err != nil { + return err + } + origin = resp.Header.Get("Access-Control-Allow-Origin") + if !(origin == "*" || origin == morigin) { + return fmt.Errorf("expected http access-control-allow-origin value '*', got '%s'", origin) + } + + return nil +} diff --git a/tests/tests_test.go b/tests/tests_test.go index d796f048..260ab9a0 100644 --- a/tests/tests_test.go +++ b/tests/tests_test.go @@ -57,6 +57,7 @@ func TestIntegration(t *testing.T) { regTestGroup("follower", subTestFollower) regTestGroup("aof", subTestAOF) regTestGroup("monitor", subTestMonitor) + regTestGroup("proto", subTestProto) runTestGroups(t) }