Skip to content

Commit

Permalink
Merge pull request #370 from supertokens/no-panic
Browse files Browse the repository at this point in the history
fix: no panic in middleware
  • Loading branch information
rishabhpoddar authored Sep 26, 2023
2 parents a311ca0 + 9257488 commit caab7e8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- It defaults to `60` or the value set in the cache-control header returned by the core
- This is optional (so you are not required to update your overrides). Returning undefined means that the header is not set.
- Handle AWS Public URLs (ending with `.amazonaws.com`) separately while extracting TLDs for SameSite attribute.
- Return `500` status instead of panic when `supertokens.Middleware` is used without initializing the SDK.
- Updates fiber adaptor package in the fiber example.

## [0.14.0] - 2023-09-11
Expand Down
33 changes: 33 additions & 0 deletions recipe/emailpassword/middleware_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package emailpassword

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/supertokens/supertokens-golang/supertokens"
)

func TestAPIWithSupertokensMiddlewareButNotInitialized(t *testing.T) {
BeforeEach()
defer AfterEach()

mux := http.NewServeMux()
testServer := httptest.NewServer(supertokens.Middleware(mux))
defer testServer.Close()

resp, err := http.Post(testServer.URL+"/auth/signup", "application/json", nil)
if err != nil {
t.Error(err.Error())
}

assert.Equal(t, 500, resp.StatusCode)
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)

bodyStr := string(bodyBytes)
assert.Equal(t, "initialisation not done. Did you forget to call the SuperTokens.init function?\n", bodyStr)
}
4 changes: 3 additions & 1 deletion supertokens/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func Init(config TypeInput) error {
func Middleware(theirHandler http.Handler) http.Handler {
instance, err := GetInstanceOrThrowError()
if err != nil {
panic("Please call supertokens.Init function before using the Middleware")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
})
}
return instance.middleware(theirHandler)
}
Expand Down

0 comments on commit caab7e8

Please sign in to comment.