From 010123c0808666a63266a913cdafc559c7254ab4 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 23 Jun 2026 14:27:09 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20add=20test=20for=20invalid=20JSO?= =?UTF-8?q?N=20JWT=20payload?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds `handler_test.go` with tests to verify the behavior of `echoHandler`. It specifically introduces a test case that passes a valid base64 string decoding to an invalid JSON payload, ensuring the error scenario is properly handled and covered. Co-authored-by: ryodocx <4597213+ryodocx@users.noreply.github.com> --- handler_test.go | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 handler_test.go diff --git a/handler_test.go b/handler_test.go new file mode 100644 index 0000000..42aa66f --- /dev/null +++ b/handler_test.go @@ -0,0 +1,85 @@ +package main + +import ( + "encoding/base64" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" +) + +func TestEchoHandler_ValidJWT(t *testing.T) { + header := base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"HS256","typ":"JWT"}`)) + payload := base64.RawURLEncoding.EncodeToString([]byte(`{"sub":"1234567890","name":"John Doe","iat":1516239022}`)) + signature := "dummy" + + jwt := header + "." + payload + "." + signature + + req := httptest.NewRequest(http.MethodGet, "/echo", nil) + req.Header.Set("Authorization", "Bearer "+jwt) + + w := httptest.NewRecorder() + echoHandler(w, req) + + res := w.Result() + defer res.Body.Close() + + if res.StatusCode != http.StatusOK { + t.Errorf("Expected status OK, got %v", res.Status) + } + + var respMap map[string]any + if err := json.NewDecoder(res.Body).Decode(&respMap); err != nil { + t.Fatalf("Failed to decode response: %v", err) + } + + authzInfo, ok := respMap["Authorization"].(map[string]any) + if !ok { + t.Fatalf("Authorization is missing or not a map") + } + + jwtInfo, ok := authzInfo["jwt"].(map[string]any) + if !ok { + t.Fatalf("jwt is missing in Authorization info") + } + + if jwtInfo["header"] == nil || jwtInfo["payload"] == nil { + t.Errorf("Expected header and payload in jwt info, got %v", jwtInfo) + } +} + +func TestEchoHandler_InvalidJSONJWT(t *testing.T) { + header := base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"HS256","typ":"JWT"}`)) + payload := base64.RawURLEncoding.EncodeToString([]byte(`{invalid_json}`)) + signature := "dummy" + + jwt := header + "." + payload + "." + signature + + req := httptest.NewRequest(http.MethodGet, "/echo", nil) + req.Header.Set("Authorization", "Bearer "+jwt) + + w := httptest.NewRecorder() + echoHandler(w, req) + + res := w.Result() + defer res.Body.Close() + + if res.StatusCode != http.StatusOK { + t.Errorf("Expected status OK, got %v", res.Status) + } + + var respMap map[string]any + if err := json.NewDecoder(res.Body).Decode(&respMap); err != nil { + t.Fatalf("Failed to decode response: %v", err) + } + + authzInfo, ok := respMap["Authorization"].(map[string]any) + if !ok { + t.Fatalf("Authorization is missing or not a map") + } + + _, ok = authzInfo["jwt"] + if ok { + t.Errorf("Expected jwt to be missing due to invalid JSON payload") + } +}