Skip to content

En/http redirects #1640

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

Open
wants to merge 28 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
83931e0
add the redirect type and method
Umang01-hash Apr 9, 2025
6ea1efa
resolve linters
Umang01-hash Apr 10, 2025
cb505b9
refcator responder.go
Umang01-hash Apr 10, 2025
29d5a9b
Merge branch 'en/http_redirects' of github.com:gofr-dev/gofr into en/…
Umang01-hash Apr 10, 2025
20b8a05
remove extra code
Umang01-hash Apr 10, 2025
53921ee
update documentation
Umang01-hash Apr 10, 2025
f8149ae
resolve linters
Umang01-hash Apr 10, 2025
ae887cc
Merge branch 'development' into en/http_redirects
Umang01-hash Apr 14, 2025
a7a956e
Merge branch 'development' of github.com:gofr-dev/gofr into en/http_r…
Umang01-hash Apr 14, 2025
5cfb815
Merge branch 'en/http_redirects' of github.com:gofr-dev/gofr into en/…
Umang01-hash Apr 14, 2025
893b18f
remove status code as a field in redirect type
Umang01-hash Apr 15, 2025
b64fb20
Merge branch 'development' of github.com:gofr-dev/gofr into en/http_r…
Umang01-hash Apr 15, 2025
1399452
fix tests
Umang01-hash Apr 15, 2025
535d760
remove Redirect method from context
Umang01-hash Apr 15, 2025
b770535
improve docs
Umang01-hash Apr 15, 2025
70318b9
resolve review comments
Umang01-hash Apr 15, 2025
1d426f9
Merge branch 'development' into en/http_redirects
Umang01-hash Apr 16, 2025
eeec99a
remove pointer return type from NewRedirect
Umang01-hash Apr 18, 2025
4edb668
Merge branch 'development' of github.com:gofr-dev/gofr into en/http_r…
Umang01-hash Apr 18, 2025
fc05212
Merge branch 'development' into en/http_redirects
Umang01-hash Apr 22, 2025
1e6a52d
Merge branch 'development' into en/http_redirects
Umang01-hash Apr 23, 2025
bc2293f
Merge branch 'development' into en/http_redirects
aryanmehrotra Apr 29, 2025
ffe4a9b
Merge branch 'development' into en/http_redirects
Umang01-hash May 2, 2025
7cd1be3
Merge branch 'en/http_redirects' of github.com:gofr-dev/gofr into en/…
Umang01-hash May 6, 2025
565e8ae
Merge branch 'development' of github.com:gofr-dev/gofr into en/http_r…
Umang01-hash May 6, 2025
43ef4ac
Merge branch 'development' into en/http_redirects
Umang01-hash May 6, 2025
9315e98
resolve review comments
Umang01-hash May 6, 2025
e6916ba
Merge branch 'en/http_redirects' of github.com:gofr-dev/gofr into en/…
Umang01-hash May 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docs/advanced-guide/overriding-default/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,33 @@ func listHandler(ctx *gofr.Context) (any, error) {
}
```

## HTTP Redirects

GoFr allows redirecting HTTP requests to other URLs using the `response.Redirect` type.

### Example

```go
package main

import (
"gofr.dev/pkg/gofr"

"gofr.dev/pkg/gofr/http/response"
)

func main() {
app := gofr.New()

app.GET("/old-page", func(ctx *gofr.Context) (any, error) {
// Redirect to a new URL
return response.Redirect{URL: "https://example.com/new-page"}, nil
})

app.Run()
}
```

## Favicon.ico

By default, GoFr load its own `favicon.ico` present in root directory for an application. To override `favicon.ico` user
Expand Down
14 changes: 14 additions & 0 deletions pkg/gofr/http/responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (r Responder) Respond(data any, err error) {
statusCode, errorObj := r.determineResponse(data, err)

var resp any

switch v := data.(type) {
case resTypes.Raw:
resp = v.Data
Expand All @@ -47,6 +48,19 @@ func (r Responder) Respond(data any, err error) {
v.Render(r.w)

return
case resTypes.Redirect:
switch r.method {
case http.MethodPost, http.MethodPut, http.MethodPatch:
statusCode = http.StatusSeeOther // 303
default:
statusCode = http.StatusFound // 302 by default
}

r.w.Header().Set("Location", v.URL)
r.w.WriteHeader(statusCode)

return

default:
// handling where an interface contains a nullable type with a nil value.
if isNil(data) {
Expand Down
36 changes: 36 additions & 0 deletions pkg/gofr/http/responder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,39 @@ func removeTemplateDir(t *testing.T) {

require.NoError(t, err)
}

func TestResponder_RedirectResponse_Post(t *testing.T) {
recorder := httptest.NewRecorder()
r := NewResponder(recorder, http.MethodPost)

// Set up redirect with specific URL and status code
redirectURL := "/new-location?from=start"
statusCode := http.StatusSeeOther // 303

redirect := resTypes.NewRedirect(redirectURL)

r.Respond(redirect, nil)

assert.Equal(t, statusCode, recorder.Code, "Redirect should set the correct status code")
assert.Equal(t, redirectURL, recorder.Header().Get("Location"),
"Redirect should set the Location header")
assert.Empty(t, recorder.Body.String(), "Redirect response should not have a body")
}

func TestResponder_RedirectResponse_Head(t *testing.T) {
recorder := httptest.NewRecorder()
r := NewResponder(recorder, http.MethodHead)

// Set up redirect with specific URL and status code
redirectURL := "/new-location?from=start"
statusCode := http.StatusFound // 302

redirect := resTypes.NewRedirect(redirectURL)

r.Respond(redirect, nil)

assert.Equal(t, statusCode, recorder.Code, "Redirect should set the correct status code")
assert.Equal(t, redirectURL, recorder.Header().Get("Location"),
"Redirect should set the Location header")
assert.Empty(t, recorder.Body.String(), "Redirect response should not have a body")
}
12 changes: 12 additions & 0 deletions pkg/gofr/http/response/redirect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package response

type Redirect struct {
URL string
}

// NewRedirect creates a redirect response with the specified URL and status code.
func NewRedirect(url string) Redirect {
return Redirect{
URL: url,
}
}
Comment on lines +7 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed.

Loading