(t *testing.T)
| 945 | } |
| 946 | |
| 947 | func TestInjectAPIKeyToMetadata(t *testing.T) { |
| 948 | gin.SetMode(gin.TestMode) |
| 949 | |
| 950 | t.Run("Non-POST request skips injection", func(t *testing.T) { |
| 951 | c, _ := gin.CreateTestContext(httptest.NewRecorder()) |
| 952 | c.Request = httptest.NewRequest("GET", "/", nil) |
| 953 | |
| 954 | err := injectAPIKeyToMetadata(c, "api_key_123") |
| 955 | assert.NoError(t, err) |
| 956 | }) |
| 957 | |
| 958 | t.Run("Nil body skips injection", func(t *testing.T) { |
| 959 | c, _ := gin.CreateTestContext(httptest.NewRecorder()) |
| 960 | c.Request = httptest.NewRequest("POST", "/", nil) |
| 961 | c.Request.Body = nil |
| 962 | |
| 963 | err := injectAPIKeyToMetadata(c, "api_key_123") |
| 964 | assert.NoError(t, err) |
| 965 | }) |
| 966 | |
| 967 | t.Run("Invalid JSON returns error", func(t *testing.T) { |
| 968 | c, _ := gin.CreateTestContext(httptest.NewRecorder()) |
| 969 | body := strings.NewReader("not valid json") |
| 970 | c.Request = httptest.NewRequest("POST", "/", body) |
| 971 | |
| 972 | err := injectAPIKeyToMetadata(c, "api_key_123") |
| 973 | assert.Error(t, err) |
| 974 | }) |
| 975 | |
| 976 | t.Run("Valid JSON without meta_data creates it", func(t *testing.T) { |
| 977 | c, _ := gin.CreateTestContext(httptest.NewRecorder()) |
| 978 | body := strings.NewReader(`{"name": "test"}`) |
| 979 | c.Request = httptest.NewRequest("POST", "/", body) |
| 980 | |
| 981 | err := injectAPIKeyToMetadata(c, "api_key_123") |
| 982 | assert.NoError(t, err) |
| 983 | }) |
| 984 | |
| 985 | t.Run("Valid JSON with existing meta_data updates it", func(t *testing.T) { |
| 986 | c, _ := gin.CreateTestContext(httptest.NewRecorder()) |
| 987 | body := strings.NewReader(`{"name": "test", "meta_data": {"existing": "value"}}`) |
| 988 | c.Request = httptest.NewRequest("POST", "/", body) |
| 989 | |
| 990 | err := injectAPIKeyToMetadata(c, "api_key_123") |
| 991 | assert.NoError(t, err) |
| 992 | }) |
| 993 | } |
nothing calls this directly
no test coverage detected