Skip to content

Commit

Permalink
Adjust routing to support handling requests with trailing forward slash
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Baranick committed May 3, 2018
1 parent 6f820e9 commit 1c52200
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,9 @@ Error Set:

## Changelog

### 0.0.4
* Adjust routing to support handling requests with trailing forward slash.

### 0.0.3
* Add HEAD support for all methods.

Expand Down
2 changes: 2 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ func (r *server) attachPrometheusMiddleware(engine *gin.Engine) {

func (r *server) handle(router *gin.Engine, relativePath string, handler gin.HandlerFunc) {
router.GET(relativePath, handler)
router.GET(relativePath+"/", handler)
router.HEAD(relativePath, handler)
router.HEAD(relativePath+"/", handler)
}

func (r *server) createClient() {
Expand Down
35 changes: 21 additions & 14 deletions server_test/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/kadaan/consulate/config"
"github.com/kadaan/consulate/testutil"
"io/ioutil"
"strings"
"testing"
"text/template"
)
Expand Down Expand Up @@ -91,49 +92,55 @@ func TestApi(t *testing.T) {
}

func verifyApiCall(t *testing.T, s *testutil.WrappedTestServer, d apiTestData) {
verifyHeadApiCall(t, s, d)
verifyGetApiCall(t, s, d)
verifyHeadApiCall(t, s, d.path, d.statusCode)
verifyHeadApiCall(t, s, appendTrailingSlash(d.path), d.statusCode)
verifyGetApiCall(t, s, d.path, d.statusCode, d.body)
verifyGetApiCall(t, s, appendTrailingSlash(d.path), d.statusCode, d.body)
}

func verifyHeadApiCall(t *testing.T, s *testutil.WrappedTestServer, d apiTestData) {
r, err := s.Client().Head(s.Url(d.path))
func appendTrailingSlash(path string) string {
return strings.Replace(path, "?", "/?", 1)
}

func verifyHeadApiCall(t *testing.T, s *testutil.WrappedTestServer, path string, statusCode int) {
r, err := s.Client().Head(s.Url(path))
if err != nil {
t.Error(err)
}
if r.StatusCode != d.statusCode {
t.Errorf("%q => StatusCode: %v, want %v", d.path, r.StatusCode, d.statusCode)
if r.StatusCode != statusCode {
t.Errorf("%q => StatusCode: %v, want %v", path, r.StatusCode, statusCode)
}
}

func verifyGetApiCall(t *testing.T, s *testutil.WrappedTestServer, d apiTestData) {
r, err := s.Client().Get(s.Url(d.path))
func verifyGetApiCall(t *testing.T, s *testutil.WrappedTestServer, path string, statusCode int, body string) {
r, err := s.Client().Get(s.Url(path))
if r != nil && r.Body != nil {
defer r.Body.Close()
}
if err != nil {
t.Error(err)
}
if r.StatusCode != d.statusCode {
t.Errorf("%q => StatusCode: %v, want %v", d.path, r.StatusCode, d.statusCode)
if r.StatusCode != statusCode {
t.Errorf("%q => StatusCode: %v, want %v", path, r.StatusCode, statusCode)
}
bb, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
body := string(bb)
actualBody := string(bb)

data := bodyTemplateData{
ConsulNodeName: s.GetConsulNodeName(),
}
tmpl, _ := template.New(d.path).Parse(d.body)
tmpl, _ := template.New(path).Parse(body)
var tpl bytes.Buffer
if err := tmpl.Execute(&tpl, data); err != nil {
t.Errorf("Failed to execute body template: %s", err)
}
expectedBody := tpl.String()

if body != expectedBody {
t.Errorf("%q => Body: %q, want %q", d.path, body, expectedBody)
if actualBody != expectedBody {
t.Errorf("%q => Body: %q, want %q", path, actualBody, expectedBody)
}
}

Expand Down

0 comments on commit 1c52200

Please sign in to comment.