Skip to content

Commit

Permalink
Merge pull request #27 from dinever/fix_redirection
Browse files Browse the repository at this point in the history
Fixes default HTTP redirection
  • Loading branch information
dinever authored Feb 24, 2017
2 parents 2a086bb + 0504d05 commit 11abebb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go:
- 1.4
- 1.5
- 1.6
- 1.7
- 1.8

before_install:
- go get github.com/mattn/goveralls
Expand Down
15 changes: 10 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (ctx *Context) Query(key string, index ...int) (string, error) {
}
return val[0], nil
}
return "", errors.New("Query key not found.")
return "", errors.New("Query key not found")
}

// Param method retrieves the parameters from url
Expand All @@ -126,9 +126,14 @@ func (ctx *Context) Param(key string) string {
return val
}

// Redirect method sets the response as a 301 redirection.
// If you need a 302 redirection, please do it by setting the Header manually.
// Redirect method sets the response as a 302 redirection.
func (ctx *Context) Redirect(url string) {
ctx.SetHeader("Location", url)
ctx.SendStatus(302)
}

// Redirect301 method sets the response as a 301 redirection.
func (ctx *Context) Redirect301(url string) {
ctx.SetHeader("Location", url)
ctx.SendStatus(301)
}
Expand Down Expand Up @@ -192,7 +197,7 @@ func (ctx *Context) Send(body interface{}) {
case *bytes.Buffer:
ctx.Response.Write(body.(*bytes.Buffer).Bytes())
default:
panic(fmt.Errorf("Body type not supported."))
panic(fmt.Errorf("Body type not supported"))
}
ctx.IsSent = true
}
Expand Down Expand Up @@ -278,7 +283,7 @@ func (ctx *Context) xsrfToken() string {
// Render a template file using the built-in Go template engine.
func (ctx *Context) Render(file string, data ...map[string]interface{}) {
if ctx.templateLoader == "" {
panic(fmt.Errorf("Template loader has not been set."))
panic(fmt.Errorf("Template loader has not been set"))
}
var renderData map[string]interface{}
if len(data) == 0 {
Expand Down
10 changes: 10 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,16 @@ func TestRedirection(t *testing.T) {
ctx := NewContext(r, w, app)
ctx.Redirect("/foo")
assertEqual(t, w.Header().Get("Location"), `/foo`)
assertEqual(t, w.Code, 302)
}

func TestRedirection301(t *testing.T) {
r := makeTestHTTPRequest(nil, "GET", "/")
w := httptest.NewRecorder()
app := New()
ctx := NewContext(r, w, app)
ctx.Redirect301("/foo")
assertEqual(t, w.Header().Get("Location"), `/foo`)
assertEqual(t, w.Code, 301)
}

Expand Down
4 changes: 2 additions & 2 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (mgr *MemorySessionManager) sessionID() (string, error) {
b := make([]byte, sessionIDLength)
n, err := rand.Read(b)
if n != len(b) || err != nil {
return "", fmt.Errorf("Could not successfully read from the system CSPRNG.")
return "", fmt.Errorf("Could not successfully read from the system CSPRNG")
}
return hex.EncodeToString(b), nil
}
Expand All @@ -53,7 +53,7 @@ func (mgr *MemorySessionManager) Session(sid string) (Session, error) {
return s, nil
}
mgr.lock.RUnlock()
return nil, fmt.Errorf("Can not retrieve session with id %s.", sid)
return nil, fmt.Errorf("Can not retrieve session with id %s", sid)
}

// NewSession creates and returns a new session.
Expand Down

0 comments on commit 11abebb

Please # to comment.