Skip to content

Commit

Permalink
Upgrade gorilla mux package and add proxy routing tests
Browse files Browse the repository at this point in the history
**What**
- Upgrade gorilla mux from 1.6.0 -> 1.6.2 so that we can use the new
SetURLVars method in unit tests
- Add additional tests for the early error cases in the proxy handler

Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
  • Loading branch information
LucasRoesler authored and alexellis committed Nov 4, 2018
1 parent cd31b38 commit 8b3147b
Show file tree
Hide file tree
Showing 13 changed files with 691 additions and 187 deletions.
10 changes: 7 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

[[constraint]]
name = "github.com/gorilla/mux"
version = "1.6.0"
version = "1.6.2"
71 changes: 71 additions & 0 deletions proxy/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
package proxy

import (
"bytes"
"errors"
"log"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"

"github.com/gorilla/mux"
)

type testBaseURLResolver struct {
testServerBase string
err error
}

func (tr *testBaseURLResolver) Resolve(name string) (url.URL, error) {
if tr.err != nil {
return url.URL{}, tr.err
}

return url.URL{
Scheme: "http",
Host: tr.testServerBase,
Expand Down Expand Up @@ -61,3 +72,63 @@ func Test_ProxyHandler_NonAllowedMethods(t *testing.T) {
})
}
}

func Test_ProxyHandler_MissingFunctionNameError(t *testing.T) {
proxyFunc := NewHandlerFunc(time.Second, &testBaseURLResolver{"", nil})

w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
req = mux.SetURLVars(req, map[string]string{"name": ""})

proxyFunc(w, req)

if w.Code != http.StatusBadRequest {
t.Errorf("expected status code `%d`, got `%d`", http.StatusBadRequest, w.Code)
}

respBody := w.Body.String()
if respBody != errMissingFunctionName {
t.Errorf("expected error message `%s`, got `%s`", errMissingFunctionName, respBody)
}
}

func Test_ProxyHandler_ResolveError(t *testing.T) {
logs := &bytes.Buffer{}
log.SetOutput(logs)

resolveErr := errors.New("can not find test service `foo`")
proxyFunc := NewHandlerFunc(time.Second, &testBaseURLResolver{"", resolveErr})

w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
req = mux.SetURLVars(req, map[string]string{"name": "foo"})

proxyFunc(w, req)

if w.Code != http.StatusNotFound {
t.Errorf("expected status code `%d`, got `%d`", http.StatusBadRequest, w.Code)
}

respBody := w.Body.String()
if respBody != "Cannot find service: foo." {
t.Errorf("expected error message `%s`, got `%s`", "Cannot find service: foo.", respBody)
}

if !strings.Contains(logs.String(), resolveErr.Error()) {
t.Errorf("expected logs to contain `%s`", resolveErr.Error())
}
}

func Test_ProxyHandler_Proxy_Success(t *testing.T) {
t.Skip("Test not implemented yet")
// testFuncService := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// w.WriteHeader(http.StatusOK)
// }))
// proxyFunc := NewHandlerFunc(time.Second, &testBaseURLResolver{testFuncService.URL, nil})

// w := httptest.NewRecorder()
// req := httptest.NewRequest("GET", "http://example.com/foo", nil)
// req = mux.SetURLVars(req, map[string]string{"name": "foo"})

// proxyFunc(w, req)
}
7 changes: 4 additions & 3 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ import (
)

const (
watchdogPort = "8080"
defaultContentType = "text/plain"
watchdogPort = "8080"
defaultContentType = "text/plain"
errMissingFunctionName = "Please provide a valid route /function/function_name."
)

// BaseURLResolver URL resolver for proxy requests
Expand Down Expand Up @@ -111,7 +112,7 @@ func proxyRequest(w http.ResponseWriter, originalReq *http.Request, proxyClient
pathVars := mux.Vars(originalReq)
functionName := pathVars["name"]
if functionName == "" {
writeError(w, http.StatusBadRequest, "Please provide a valid route /function/function_name.")
writeError(w, http.StatusBadRequest, errMissingFunctionName)
return
}

Expand Down
11 changes: 6 additions & 5 deletions vendor/github.com/gorilla/mux/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions vendor/github.com/gorilla/mux/ISSUE_TEMPLATE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8b3147b

Please sign in to comment.