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
package auth import ( "context" "fmt" "net/http" "net/http/httptest" "testing" "github.com/stretchr/testify/assert" ) const ( validToken = "valid_token" invalidToken = "invalid_token" expiredToken = "expired_token" ) var config = struct { Connection_Auth string }{ Connection_Auth: validToken, } type contextKey string const ContextKey contextKey = "token" func TestConnectionCodeContext(t *testing.T) { tests := []struct { name string token string expectedStatus int expectedLog string expectNextCall bool }{ { name: "Valid Token in Header", token: validToken, expectedStatus: http.StatusOK, expectedLog: "", expectNextCall: true, }, { name: "Empty Token in Header", token: "", expectedStatus: http.StatusUnauthorized, expectedLog: "[auth] no token", expectNextCall: false, }, { name: "Invalid Token in Header", token: invalidToken, expectedStatus: http.StatusUnauthorized, expectedLog: "Not a super admin : auth", expectNextCall: false, }, { name: "Missing Token Header", token: "", expectedStatus: http.StatusUnauthorized, expectedLog: "[auth] no token", expectNextCall: false, }, { name: "Token with Special Characters", token: "valid_token!@#$%^&*()", expectedStatus: http.StatusUnauthorized, expectedLog: "Not a super admin : auth", expectNextCall: false, }, { name: "Token with Leading/Trailing Spaces", token: " " + validToken + " ", expectedStatus: http.StatusUnauthorized, expectedLog: "Not a super admin : auth", expectNextCall: false, }, { name: "Token Case Sensitivity", token: "VALID_TOKEN", expectedStatus: http.StatusUnauthorized, expectedLog: "Not a super admin : auth", expectNextCall: false, }, { name: "Token with Unicode Characters", token: "valid_token_üñîçødê", expectedStatus: http.StatusUnauthorized, expectedLog: "Not a super admin : auth", expectNextCall: false, }, { name: "Expired Token", token: expiredToken, expectedStatus: http.StatusUnauthorized, expectedLog: "Token expired", expectNextCall: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Create a mock next handler nextCalled := false nextHandler := 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 rr := httptest.NewRecorder() // Capture log output logOutput := "" fmtPrintln := fmt.Println fmt.Println = func(a ...interface{}) (n int, err error) { logOutput = fmt.Sprint(a...) return 0, nil } defer func() { fmt.Println = fmtPrintln }() // Call the function under test handler := ConnectionCodeContext(nextHandler) 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) // Check the log output if tt.expectedLog != "" { assert.Contains(t, logOutput, tt.expectedLog) } }) } }
Completeness:
Correctness:
next
Consistency with Test Cases:
Code Structure and Clarity:
assert
This finalized code ensures comprehensive coverage of the ConnectionCodeContext function, addressing all specified scenarios and conditions.
ConnectionCodeContext
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Unit Test Coverage for "ConnectionCodeContext"
Stakwork Run
Unit Test Code
Explanation of Adjustments:
Completeness:
Correctness:
next
handler is called.Consistency with Test Cases:
Code Structure and Clarity:
assert
for clear and concise assertions.This finalized code ensures comprehensive coverage of the
ConnectionCodeContext
function, addressing all specified scenarios and conditions.The text was updated successfully, but these errors were encountered: