-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Adds test for http auth middleware #2243
New issue
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
687b330
adds test for http auth middleware
gizmo-rt e19dd83
Merge branch 'development' into tests/auth-middleware
gizmo-rt 3024c09
uses server configs for dynamic ports
gizmo-rt ee04e4b
Merge branch 'development' into tests/auth-middleware
gizmo-rt c5b1db9
Merge branch 'development' into tests/auth-middleware
gizmo-rt 4ff5015
commented tests for optional code in example
coolwednesday 6a5abb4
Merge branch 'development' into tests/auth-middleware
coolwednesday fbc5892
Merge branch 'development' into tests/auth-middleware
Umang01-hash 78b10b0
Merge branch 'development' into tests/auth-middleware
Umang01-hash 6c2fe70
updates tests inline with proposed requests
gizmo-rt db16e7b
Merge branch 'development' into tests/auth-middleware
gizmo-rt 0a1d435
Merge branch 'development' into tests/auth-middleware
Umang01-hash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "fmt" | ||
| "github.com/stretchr/testify/assert" | ||
| "gofr.dev/pkg/gofr" | ||
| "net/http" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func Test_main(t *testing.T) { | ||
| go main() | ||
| } | ||
|
|
||
| func Test_setupBasicAuthSuccess(t *testing.T) { | ||
| os.Setenv("METRICS_PORT", "2200") | ||
| os.Setenv("HTTP_PORT", "8200") | ||
| app := gofr.New() | ||
| setupBasicAuth(app) | ||
| app.GET("/basic-auth-success", func(_ *gofr.Context) (any, error) { | ||
| return "success", nil | ||
| }) | ||
| go app.Run() | ||
| time.Sleep(100 * time.Millisecond) | ||
|
|
||
| var netClient = &http.Client{ | ||
| Timeout: 200 * time.Millisecond, | ||
| } | ||
|
|
||
| req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet, | ||
| fmt.Sprintf("http://localhost:%d", 8200)+"/basic-auth-success", http.NoBody) | ||
| req.Header.Add("Authorization", encodeBasicAuthorization(t, "username:password")) | ||
|
|
||
| // Send the request and check for successful response | ||
| resp, err := netClient.Do(req) | ||
| if err != nil { | ||
| t.Errorf("error while making HTTP request in Test_BasicAuthMiddleware. err: %v", err) | ||
| return | ||
| } | ||
|
|
||
| defer resp.Body.Close() | ||
|
|
||
| assert.Equal(t, http.StatusOK, resp.StatusCode, "Test_setupBasicAuthSuccess") | ||
| } | ||
|
|
||
| func Test_setupBasicAuthFailed(t *testing.T) { | ||
| os.Setenv("METRICS_PORT", "2201") | ||
| os.Setenv("HTTP_PORT", "8201") | ||
| app := gofr.New() | ||
| setupBasicAuth(app) | ||
| app.GET("/basic-auth-failure", func(_ *gofr.Context) (any, error) { | ||
| return "success", nil | ||
| }) | ||
| go app.Run() | ||
| time.Sleep(100 * time.Millisecond) | ||
|
|
||
| var netClient = &http.Client{ | ||
| Timeout: 200 * time.Millisecond, | ||
| } | ||
|
|
||
| req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet, | ||
| fmt.Sprintf("http://localhost:%d", 8201)+"/basic-auth-failure", http.NoBody) | ||
| req.Header.Add("Authorization", encodeBasicAuthorization(t, "username")) | ||
|
|
||
| // Send the request and check for successful response | ||
| resp, err := netClient.Do(req) | ||
| if err != nil { | ||
| t.Errorf("error while making HTTP request in Test_BasicAuthMiddleware. err: %v", err) | ||
| return | ||
| } | ||
|
|
||
| defer resp.Body.Close() | ||
|
|
||
| assert.Equal(t, http.StatusUnauthorized, resp.StatusCode, "Test_setupBasicAuthFailed") | ||
| } | ||
|
|
||
| func Test_setupAPIKeyAuthFailed(t *testing.T) { | ||
| os.Setenv("METRICS_PORT", "2100") | ||
| os.Setenv("HTTP_PORT", "8100") | ||
| app := gofr.New() | ||
| setupAPIKeyAuth(app) | ||
| app.GET("/api-key-failure", func(_ *gofr.Context) (any, error) { | ||
| return "success", nil | ||
| }) | ||
| go app.Run() | ||
| time.Sleep(100 * time.Millisecond) | ||
|
|
||
| var netClient = &http.Client{ | ||
| Timeout: 200 * time.Millisecond, | ||
| } | ||
|
|
||
| req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet, | ||
| fmt.Sprintf("http://localhost:%d", 8100)+"/api-key-failure", http.NoBody) | ||
| req.Header.Set("X-Api-Key", "test-key") | ||
|
|
||
| // Send the request and check for successful response | ||
| resp, err := netClient.Do(req) | ||
| if err != nil { | ||
| t.Errorf("error while making HTTP request in Test_APIKeyAuthMiddleware. err: %v", err) | ||
| return | ||
| } | ||
|
|
||
| defer resp.Body.Close() | ||
|
|
||
| assert.Equal(t, http.StatusUnauthorized, resp.StatusCode, "Test_setupAPIKeyAuthFailed") | ||
| } | ||
|
|
||
| func Test_setupAPIKeyAuthSuccess(t *testing.T) { | ||
| os.Setenv("METRICS_PORT", "2101") | ||
| os.Setenv("HTTP_PORT", "8101") | ||
| app := gofr.New() | ||
| setupAPIKeyAuth(app) | ||
| app.GET("/api-key-success", func(_ *gofr.Context) (any, error) { | ||
| return "success", nil | ||
| }) | ||
| go app.Run() | ||
| time.Sleep(100 * time.Millisecond) | ||
|
|
||
| var netClient = &http.Client{ | ||
| Timeout: 200 * time.Millisecond, | ||
| } | ||
|
|
||
| req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet, | ||
| fmt.Sprintf("http://localhost:%d", 8101)+"/api-key-success", http.NoBody) | ||
| req.Header.Set("X-Api-Key", "valid-api-key") | ||
|
|
||
| // Send the request and check for successful response | ||
| resp, err := netClient.Do(req) | ||
| if err != nil { | ||
| t.Errorf("error while making HTTP request in Test_APIKeyAuthMiddleware. err: %v", err) | ||
| return | ||
| } | ||
|
|
||
| defer resp.Body.Close() | ||
|
|
||
| assert.Equal(t, http.StatusOK, resp.StatusCode, "Test_setupAPIKeyAuthSuccess") | ||
| } | ||
|
|
||
| func encodeBasicAuthorization(t *testing.T, arg string) string { | ||
| t.Helper() | ||
|
|
||
| data := []byte(arg) | ||
|
|
||
| dst := make([]byte, base64.StdEncoding.EncodedLen(len(data))) | ||
|
|
||
| base64.StdEncoding.Encode(dst, data) | ||
|
|
||
| s := "Basic " + string(dst) | ||
|
|
||
| return s | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.