We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stakwork Run
File: /tmp/stakwork/sphinx-tribes/auth/auth.go
package auth import ( "context" "net/http" "net/http/httptest" "strings" "testing" "github.com/stretchr/testify/assert" ) func TestConnectionCodeContext(t *testing.T) { // Mock configuration for testing config.Connection_Auth = "valid_token" tests := []struct { name string token string expectedStatus int expectNextCall bool }{ { name: "Valid Token in Header", token: "valid_token", expectedStatus: http.StatusOK, expectNextCall: true, }, { name: "Invalid Token in Header", token: "invalid_token", expectedStatus: http.StatusUnauthorized, expectNextCall: false, }, { name: "Empty Token in Header", token: "", expectedStatus: http.StatusUnauthorized, expectNextCall: false, }, { name: "No Token Header Present", token: "", expectedStatus: http.StatusUnauthorized, expectNextCall: false, }, { name: "Malformed Header", token: "malformed_header", expectedStatus: http.StatusUnauthorized, expectNextCall: false, }, { name: "Token with Special Characters", token: "special!@#token", expectedStatus: http.StatusUnauthorized, expectNextCall: false, }, { name: "Token with Whitespace", token: " " + config.Connection_Auth + " ", expectedStatus: http.StatusUnauthorized, expectNextCall: false, }, { name: "Case Sensitivity in Token", token: strings.ToUpper(config.Connection_Auth), expectedStatus: http.StatusUnauthorized, expectNextCall: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Create a mock handler to verify if it gets called nextCalled := false next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { nextCalled = true w.WriteHeader(http.StatusOK) }) // Create a request with the specified token req := httptest.NewRequest(http.MethodGet, "/", nil) if tt.token != "" { req.Header.Set("token", tt.token) } // Create a response recorder to capture the response rr := httptest.NewRecorder() // Call the function under test handler := ConnectionCodeContext(next) handler.ServeHTTP(rr, req) // Check the response status code assert.Equal(t, tt.expectedStatus, rr.Code) // Check if the next handler was called assert.Equal(t, tt.expectNextCall, nextCalled) }) } t.Run("Null Request Object", func(t *testing.T) { // Create a mock handler to verify if it gets called nextCalled := false next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { nextCalled = true w.WriteHeader(http.StatusOK) }) // Call the function under test with a nil request handler := ConnectionCodeContext(next) rr := httptest.NewRecorder() handler.ServeHTTP(rr, nil) // Check the response status code assert.Equal(t, http.StatusInternalServerError, rr.Code) // Check if the next handler was not called assert.False(t, nextCalled) }) t.Run("Large Number of Requests", func(t *testing.T) { // Create a mock handler to verify if it gets called nextCalled := 0 next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { nextCalled++ w.WriteHeader(http.StatusOK) }) // Simulate a high volume of requests for i := 0; i < 1000; i++ { req := httptest.NewRequest(http.MethodGet, "/", nil) if i%2 == 0 { req.Header.Set("token", "valid_token") } else { req.Header.Set("token", "invalid_token") } rr := httptest.NewRecorder() handler := ConnectionCodeContext(next) handler.ServeHTTP(rr, req) if i%2 == 0 { assert.Equal(t, http.StatusOK, rr.Code) } else { assert.Equal(t, http.StatusUnauthorized, rr.Code) } } // Check if the next handler was called the expected number of times assert.Equal(t, 500, nextCalled) }) }
The text was updated successfully, but these errors were encountered:
@tomsmith8 I help?
Sorry, something went wrong.
sophieturner0
Successfully merging a pull request may close this issue.
Unit Test Coverage for "ConnectionCodeContext"
Stakwork Run
Unit Test Code
File: /tmp/stakwork/sphinx-tribes/auth/auth.go
The text was updated successfully, but these errors were encountered: