Skip to content

Conversation

@gizmo-rt
Copy link
Contributor

@gizmo-rt gizmo-rt commented Sep 8, 2025

Pull Request Template

Description:

Adds test for http auth middleware example

Breaking Changes (if applicable):

  • List any breaking changes introduced by this PR. N/A
  • Explain the rationale behind these changes and how they will impact users.

Additional Information:

  • Mention any relevant dependencies or external libraries used.
  • Include screenshots or code snippets (if necessary) to clarify the changes.

Checklist:

  • I have formatted my code using goimport and golangci-lint.
  • All new code is covered by unit tests.
  • This PR does not decrease the overall code coverage.
  • I have reviewed the code comments and documentation for clarity.

Thank you for your contribution!

Copy link
Member

@Umang01-hash Umang01-hash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2025-09-12 at 12 24 26 PM

Hey @gizmo-rt!
The test you added is only adding a coverage of 30% to the file main.go. The middlewares code are commented somehow and we only have one handler registered. Can you please try and uncomment the code to write test for it along with handler.

@gizmo-rt
Copy link
Contributor Author

gizmo-rt commented Sep 20, 2025

@Umang01-hash The commented example seems like an alternative method to use the auth middleware.
Either we can keep one of those examples or decouple and include both. Thoughts ?

coolwednesday
coolwednesday previously approved these changes Sep 24, 2025
@Umang01-hash
Copy link
Member

@gizmo-rt what i meant to say was we should add tests that directly cover the main() function. You don't need to uncomment the alternatives - instead, add a test like this:

func Test_MainEndToEnd(t *testing.T) {
    serverConfigs := testutil.NewServerConfigs(t)
    
    // Run main() in a goroutine to avoid blocking
    go main()
    
    // Allow time for server to start
    time.Sleep(100 * time.Millisecond)
    
    client := &http.Client{Timeout: 200 * time.Millisecond}
    
    // Test valid API key
    t.Run("Valid API Key", func(t *testing.T) {
        req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet,
            serverConfigs.HTTPHost+"/test-auth", http.NoBody)
        req.Header.Set("X-Api-Key", "valid-api-key")
        
        resp, err := client.Do(req)
        if err != nil {
            t.Fatalf("Error making request: %v", err)
        }
        defer resp.Body.Close()
        
        assert.Equal(t, http.StatusOK, resp.StatusCode)
    })

}

@gizmo-rt gizmo-rt requested a review from coolwednesday October 3, 2025 08:01
@Umang01-hash Umang01-hash merged commit 2bee027 into development Oct 6, 2025
19 checks passed
@Umang01-hash Umang01-hash deleted the tests/auth-middleware branch October 6, 2025 05:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants