diff --git a/cmd/server/main.go b/cmd/server/main.go index 2bcd277..defd875 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -17,6 +17,7 @@ import ( "github.com/speakeasy-api/speakeasy-api-test-service/internal/middleware" "github.com/speakeasy-api/speakeasy-api-test-service/internal/pagination" "github.com/speakeasy-api/speakeasy-api-test-service/internal/readonlywriteonly" + "github.com/speakeasy-api/speakeasy-api-test-service/internal/redirects" "github.com/speakeasy-api/speakeasy-api-test-service/internal/reflect" "github.com/speakeasy-api/speakeasy-api-test-service/internal/responseHeaders" "github.com/speakeasy-api/speakeasy-api-test-service/internal/retries" @@ -86,6 +87,8 @@ func main() { r.HandleFunc("/method/post", method.HandlePost).Methods(http.MethodPost) r.HandleFunc("/method/put", method.HandlePut).Methods(http.MethodPut) r.HandleFunc("/method/trace", method.HandleTrace).Methods(http.MethodTrace) + r.HandleFunc("/followRedirect/oldPage", redirects.HandleRedirectOldPage).Methods(http.MethodGet) + r.HandleFunc("/followRedirect/newPage", redirects.HandleRedirectNewPage).Methods(http.MethodGet) oauth2router := r.NewRoute().Subrouter() oauth2router.Use(middleware.OAuth2) diff --git a/internal/redirects/service.go b/internal/redirects/service.go new file mode 100644 index 0000000..acec25c --- /dev/null +++ b/internal/redirects/service.go @@ -0,0 +1,29 @@ +package redirects + +import ( + "encoding/json" + "net/http" +) + +// HandleRedirectOldPage handles the /followRedirect endpoint that returns a 302 redirect +func HandleRedirectOldPage(w http.ResponseWriter, r *http.Request) { + // Set the Location header to the redirect target + w.Header().Set("Location", "/followRedirect/newPage") + // Return 302 Found status + w.WriteHeader(http.StatusFound) +} + +// HandleRedirectNewPage handles the /followRedirect/newPage endpoint that returns a 200 with JSON +func HandleRedirectNewPage(w http.ResponseWriter, r *http.Request) { + // Set content type to application/json + w.Header().Set("Content-Type", "application/json") + + // Create the response object according to the OpenAPI spec + response := map[string]string{ + "name": "John Doe", + } + + // Encode and send the JSON response + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(response) +}