package handler import ( "encoding/json" "mchess_server/api" "mchess_server/utils" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" ) func Test_GetLobbyFromPassphraseHandler(t *testing.T) { var passphraseURLParameter string var receivedPhrase string t.Run("host a lobby", func(t *testing.T) { r1 := httptest.NewRecorder() ctx1, e1 := gin.CreateTestContext(r1) e1.GET("/api/hostPrivate", HostGameHandler) hostGameRequest, _ := http.NewRequest("GET", "/api/hostPrivate", nil) ctx1.Request = hostGameRequest e1.ServeHTTP(r1, hostGameRequest) playerInfo := api.PlayerInfo{} err := json.Unmarshal(r1.Body.Bytes(), &playerInfo) assert.NoError(t, err) receivedPhrase = *playerInfo.Passphrase passphrase := utils.NewPassphraseFromString(receivedPhrase) passphraseURLParameter = passphrase.AsURLParam() }) t.Run("see if the lobby can be fetched by using the passphrase", func(t *testing.T) { r2 := httptest.NewRecorder() ctx2, engine := gin.CreateTestContext(r2) engine.GET("/api/getLobbyForPassphrase/:phrase", GetLobbyForPassphraseHandler) url := "/api/getLobbyForPassphrase/" + passphraseURLParameter getLobbyRequest, _ := http.NewRequest("GET", url, nil) ctx2.Request = getLobbyRequest engine.ServeHTTP(r2, getLobbyRequest) passhrase := api.Passphrase{} err := json.Unmarshal(r2.Body.Bytes(), &passhrase) assert.NoError(t, err) assert.Equal(t, http.StatusOK, r2.Code) }) }