Skip to content

Commit 57a7215

Browse files
Merge pull request #300 from supertokens/testing/additional-tests
chore: Add test for verify session without using middleware
2 parents c7f4847 + e7f1f2c commit 57a7215

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

recipe/session/verifySession_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,39 @@ func TestThatVerifySessionDoesNotAlwaysCallCore(t *testing.T) {
892892
assert.True(t, didGetSessionCallCore)
893893
}
894894

895+
func TestThatVerifySessionReturns401IfNoAccessTokenIsSentAndMiddlewareIsNotAdded(t *testing.T) {
896+
configValue := supertokens.TypeInput{
897+
Supertokens: &supertokens.ConnectionInfo{
898+
ConnectionURI: "http://localhost:8080",
899+
},
900+
AppInfo: supertokens.AppInfo{
901+
AppName: "SuperTokens",
902+
WebsiteDomain: "supertokens.io",
903+
APIDomain: "api.supertokens.io",
904+
},
905+
RecipeList: []supertokens.Recipe{
906+
Init(nil),
907+
},
908+
}
909+
BeforeEach()
910+
unittesting.StartUpST("localhost", "8080")
911+
defer AfterEach()
912+
err := supertokens.Init(configValue)
913+
if err != nil {
914+
t.Error(err.Error())
915+
}
916+
917+
testServer := getTestServerWithoutMiddleware()
918+
bodyBytes := []byte("{}")
919+
res, err := http.Post(testServer.URL+"/verify", "application/json", bytes.NewBuffer(bodyBytes))
920+
921+
if err != nil {
922+
t.Error(err.Error())
923+
}
924+
925+
assert.Equal(t, res.StatusCode, 401)
926+
}
927+
895928
type typeTestEndpoint struct {
896929
path string
897930
overrideGlobalClaimValidators func(globalClaimValidators []claims.SessionClaimValidator, sessionContainer sessmodels.SessionContainer, userContext supertokens.UserContext) ([]claims.SessionClaimValidator, error)
@@ -995,3 +1028,25 @@ func getTestApp(endpoints []typeTestEndpoint) *httptest.Server {
9951028
testServer := httptest.NewServer(supertokens.Middleware(mux))
9961029
return testServer
9971030
}
1031+
1032+
func getTestServerWithoutMiddleware() *httptest.Server {
1033+
mux := http.NewServeMux()
1034+
1035+
mux.HandleFunc("/verify", VerifySession(nil, func(w http.ResponseWriter, r *http.Request) {
1036+
sessionContainer := GetSessionFromRequestContext(r.Context())
1037+
resp := map[string]interface{}{
1038+
"message": sessionContainer.GetHandle(),
1039+
}
1040+
respBytes, err := json.Marshal(resp)
1041+
if err != nil {
1042+
return
1043+
}
1044+
w.Header().Set("Content-Type", "application/json")
1045+
w.Header().Set("Content-Length", fmt.Sprintf("%d", (len(respBytes))))
1046+
w.WriteHeader(http.StatusOK)
1047+
w.Write(respBytes)
1048+
}))
1049+
1050+
testServer := httptest.NewServer(mux)
1051+
return testServer
1052+
}

0 commit comments

Comments
 (0)