Skip to content

Commit

Permalink
Merge pull request #7 from themue/version-2-12-0
Browse files Browse the repository at this point in the history
Version 2 12 0
  • Loading branch information
Frank Mueller authored Feb 12, 2017
2 parents 4184fc0 + 7931167 commit 6dec938
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 151 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Tideland Go REST Server Library

## 2017-02-12

- Some renamings in *Request* and *Response*, sadly
to the previous minor release
- More convenience helpers for testing
- Adopted new testing to more packages
- Using http package constants instead of own
plain strings
- Added documentation to restaudit

## 2017-02-10

- Extended *Request* and *Response* of *restaudit* with some
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ I hope you like it. ;)

## Version

Version 2.11.0
Version 2.12.0

## Packages

Expand Down
39 changes: 15 additions & 24 deletions handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ func TestWrapperHandler(t *testing.T) {
err := mux.Register("test", "wrapper", handlers.NewWrapperHandler("wrapper", handler))
assert.Nil(err)
// Perform test requests.
resp := ts.DoRequest(&restaudit.Request{
Method: "GET",
Path: "/test/wrapper",
})
assert.Equal(string(resp.Body), data)
req := restaudit.NewRequest("GET", "/test/wrapper")
resp := ts.DoRequest(req)
resp.AssertBodyContains(data)
}

// TestFileServeHandler tests the serving of files.
Expand All @@ -81,16 +79,12 @@ func TestFileServeHandler(t *testing.T) {
err = mux.Register("test", "files", handlers.NewFileServeHandler("files", dir))
assert.Nil(err)
// Perform test requests.
resp := ts.DoRequest(&restaudit.Request{
Method: "GET",
Path: "/test/files/foo.txt",
})
assert.Equal(string(resp.Body), data)
resp = ts.DoRequest(&restaudit.Request{
Method: "GET",
Path: "/test/files/does.not.exist",
})
assert.Equal(string(resp.Body), "404 page not found\n")
req := restaudit.NewRequest("GET", "/test/files/foo.txt")
resp := ts.DoRequest(req)
resp.AssertBodyContains(data)
req = restaudit.NewRequest("GET", "/test/files/does.not.exist")
resp = ts.DoRequest(req)
resp.AssertBodyContains("404 page not found")
}

// TestFileUploadHandler tests the uploading of files.
Expand Down Expand Up @@ -247,24 +241,21 @@ func TestJWTAuthorizationHandler(t *testing.T) {
err := mux.Register("jwt", test.id, handlers.NewAuditHandler("audit", assert, test.auditf))
assert.Nil(err)
}
var requestProcessor func(req *http.Request) *http.Request
// Create request.
req := restaudit.NewRequest("GET", "/jwt/"+test.id+"/1234567890")
if test.tokener != nil {
requestProcessor = func(req *http.Request) *http.Request {
req.SetRequestProcessor(func(req *http.Request) *http.Request {
return jwt.AddTokenToRequest(req, test.tokener())
}
})
}
// Make request(s).
runs := 1
if test.runs != 0 {
runs = test.runs
}
for i := 0; i < runs; i++ {
resp := ts.DoRequest(&restaudit.Request{
Method: "GET",
Path: "/jwt/" + test.id + "/1234567890",
RequestProcessor: requestProcessor,
})
assert.Equal(resp.Status, test.status)
resp := ts.DoRequest(req)
resp.AssertStatusEquals(test.status)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion jwt/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Tideland Go REST Server Library - JSON Web Token - Errors
//
// Copyright (C) 2016 Frank Mueller / Tideland / Oldenburg / Germany
// Copyright (C) 2016-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
Expand Down
96 changes: 34 additions & 62 deletions jwt/header_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Tideland Go REST Server Library - JSON Web Token - Unit Tests
//
// Copyright (C) 2016 Frank Mueller / Tideland / Oldenburg / Germany
// Copyright (C) 2016-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
Expand All @@ -13,7 +13,6 @@ package jwt_test

import (
"context"
"encoding/json"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -46,17 +45,14 @@ func TestDecodeRequest(t *testing.T) {
err = mux.Register("test", "jwt", NewTestHandler("jwt", assert, nil, false))
assert.Nil(err)
// Perform test request.
resp := ts.DoRequest(&restaudit.Request{
Method: "GET",
Path: "/test/jwt/1234567890",
Header: restaudit.KeyValues{"Accept": "application/json"},
RequestProcessor: func(req *http.Request) *http.Request {
return jwt.AddTokenToRequest(req, jwtIn)
},
req := restaudit.NewRequest("GET", "/test/jwt/1234567890")
req.AddHeader(restaudit.HeaderAccept, restaudit.ApplicationJSON)
req.SetRequestProcessor(func(req *http.Request) *http.Request {
return jwt.AddTokenToRequest(req, jwtIn)
})
var claimsOut jwt.Claims
err = json.Unmarshal(resp.Body, &claimsOut)
assert.Nil(err)
resp := ts.DoRequest(req)
claimsOut := jwt.Claims{}
resp.AssertUnmarshalledBody(&claimsOut)
assert.Equal(claimsOut, claimsIn)
}

Expand All @@ -76,29 +72,19 @@ func TestDecodeCachedRequest(t *testing.T) {
err = mux.Register("test", "jwt", NewTestHandler("jwt", assert, nil, true))
assert.Nil(err)
// Perform first test request.
resp := ts.DoRequest(&restaudit.Request{
Method: "GET",
Path: "/test/jwt/1234567890",
Header: restaudit.KeyValues{"Accept": "application/json"},
RequestProcessor: func(req *http.Request) *http.Request {
return jwt.AddTokenToRequest(req, jwtIn)
},
req := restaudit.NewRequest("GET", "/test/jwt/1234567890")
req.AddHeader(restaudit.HeaderAccept, restaudit.ApplicationJSON)
req.SetRequestProcessor(func(req *http.Request) *http.Request {
return jwt.AddTokenToRequest(req, jwtIn)
})
var claimsOut jwt.Claims
err = json.Unmarshal(resp.Body, &claimsOut)
assert.Nil(err)
resp := ts.DoRequest(req)
claimsOut := jwt.Claims{}
resp.AssertUnmarshalledBody(&claimsOut)
assert.Equal(claimsOut, claimsIn)
// Perform second test request.
resp = ts.DoRequest(&restaudit.Request{
Method: "GET",
Path: "/test/jwt/1234567890",
Header: restaudit.KeyValues{"Accept": "application/json"},
RequestProcessor: func(req *http.Request) *http.Request {
return jwt.AddTokenToRequest(req, jwtIn)
},
})
err = json.Unmarshal(resp.Body, &claimsOut)
assert.Nil(err)
resp = ts.DoRequest(req)
claimsOut = jwt.Claims{}
resp.AssertUnmarshalledBody(&claimsOut)
assert.Equal(claimsOut, claimsIn)
}

Expand All @@ -118,17 +104,14 @@ func TestVerifyRequest(t *testing.T) {
err = mux.Register("test", "jwt", NewTestHandler("jwt", assert, key, false))
assert.Nil(err)
// Perform test request.
resp := ts.DoRequest(&restaudit.Request{
Method: "GET",
Path: "/test/jwt/1234567890",
Header: restaudit.KeyValues{"Accept": "application/json"},
RequestProcessor: func(req *http.Request) *http.Request {
return jwt.AddTokenToRequest(req, jwtIn)
},
req := restaudit.NewRequest("GET", "/test/jwt/1234567890")
req.AddHeader(restaudit.HeaderAccept, restaudit.ApplicationJSON)
req.SetRequestProcessor(func(req *http.Request) *http.Request {
return jwt.AddTokenToRequest(req, jwtIn)
})
var claimsOut jwt.Claims
err = json.Unmarshal(resp.Body, &claimsOut)
assert.Nil(err)
resp := ts.DoRequest(req)
claimsOut := jwt.Claims{}
resp.AssertUnmarshalledBody(&claimsOut)
assert.Equal(claimsOut, claimsIn)
}

Expand All @@ -148,29 +131,18 @@ func TestVerifyCachedRequest(t *testing.T) {
err = mux.Register("test", "jwt", NewTestHandler("jwt", assert, key, true))
assert.Nil(err)
// Perform first test request.
resp := ts.DoRequest(&restaudit.Request{
Method: "GET",
Path: "/test/jwt/1234567890",
Header: restaudit.KeyValues{"Accept": "application/json"},
RequestProcessor: func(req *http.Request) *http.Request {
return jwt.AddTokenToRequest(req, jwtIn)
},
req := restaudit.NewRequest("GET", "/test/jwt/1234567890")
req.AddHeader(restaudit.HeaderAccept, restaudit.ApplicationJSON)
req.SetRequestProcessor(func(req *http.Request) *http.Request {
return jwt.AddTokenToRequest(req, jwtIn)
})
var claimsOut jwt.Claims
err = json.Unmarshal(resp.Body, &claimsOut)
assert.Nil(err)
resp := ts.DoRequest(req)
claimsOut := jwt.Claims{}
resp.AssertUnmarshalledBody(&claimsOut)
assert.Equal(claimsOut, claimsIn)
// Perform second test request.
resp = ts.DoRequest(&restaudit.Request{
Method: "GET",
Path: "/test/jwt/1234567890",
Header: restaudit.KeyValues{"Accept": "application/json"},
RequestProcessor: func(req *http.Request) *http.Request {
return jwt.AddTokenToRequest(req, jwtIn)
},
})
err = json.Unmarshal(resp.Body, &claimsOut)
assert.Nil(err)
resp = ts.DoRequest(req)
resp.AssertUnmarshalledBody(&claimsOut)
assert.Equal(claimsOut, claimsIn)
}

Expand Down
15 changes: 8 additions & 7 deletions rest/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package rest

import (
"fmt"
"net/http"

"github.com/tideland/golib/errors"
)
Expand Down Expand Up @@ -83,43 +84,43 @@ func handleJob(handler ResourceHandler, job Job) (bool, error) {
return fmt.Sprintf("%s@%s/%s", handler.ID(), job.Domain(), job.Resource())
}
switch job.Request().Method {
case "GET":
case http.MethodGet:
grh, ok := handler.(GetResourceHandler)
if !ok {
return false, errors.New(ErrNoGetHandler, errorMessages, id())
}
return grh.Get(job)
case "HEAD":
case http.MethodHead:
hrh, ok := handler.(HeadResourceHandler)
if !ok {
return false, errors.New(ErrNoHeadHandler, errorMessages, id())
}
return hrh.Head(job)
case "PUT":
case http.MethodPut:
prh, ok := handler.(PutResourceHandler)
if !ok {
return false, errors.New(ErrNoPutHandler, errorMessages, id())
}
return prh.Put(job)
case "POST":
case http.MethodPost:
prh, ok := handler.(PostResourceHandler)
if !ok {
return false, errors.New(ErrNoPostHandler, errorMessages, id())
}
return prh.Post(job)
case "PATCH":
case http.MethodPatch:
prh, ok := handler.(PatchResourceHandler)
if !ok {
return false, errors.New(ErrNoPatchHandler, errorMessages, id())
}
return prh.Patch(job)
case "DELETE":
case http.MethodDelete:
drh, ok := handler.(DeleteResourceHandler)
if !ok {
return false, errors.New(ErrNoDeleteHandler, errorMessages, id())
}
return drh.Delete(job)
case "OPTIONS":
case http.MethodOptions:
orh, ok := handler.(OptionsResourceHandler)
if !ok {
return false, errors.New(ErrNoOptionsHandler, errorMessages, id())
Expand Down
Loading

0 comments on commit 6dec938

Please sign in to comment.