Skip to content

Commit

Permalink
More linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin committed Jul 10, 2014
1 parent dc085f0 commit 083eae6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
7 changes: 6 additions & 1 deletion handler_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"time"
)

// APIError is generic error object returned if there is something wrong with the request
type APIError struct {
Message string
}

// ErrorHandler is invoked whenever there is an issue with a proxied request, most middleware will invoke
// the ErrorHandler if something is wrong with the request and halt the request processing through the chain
type ErrorHandler struct {
Expand Down Expand Up @@ -51,7 +56,7 @@ func (e ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, err st
w.WriteHeader(errCode)
w.Header().Add("Content-Type", "application/json")
w.Header().Add("X-Generator", "tyk.io")
thisError := ApiError{fmt.Sprintf("%s", err)}
thisError := APIError{fmt.Sprintf("%s", err)}
templates.ExecuteTemplate(w, "error.json", &thisError)
if doMemoryProfile {
pprof.WriteHeapProfile(prof_file)
Expand Down
6 changes: 1 addition & 5 deletions handler_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import (
"net/http/httputil"
)

type ApiError struct {
Message string
}

// Proxies request onwards
// ProxyHandler Proxies requests through to their final destination, if they make it through the middleware chain.
func ProxyHandler(p *httputil.ReverseProxy, apiSpec APISpec) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {

Expand Down
9 changes: 9 additions & 0 deletions handler_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,31 @@ import (
"time"
)

// ContextKey is a key type to avoid collisions
type ContextKey int

// Enums for keys to be stored in a session context - this is how gorilla expects
// these to be implemented and is lifted pretty much from docs
const (
SessionData = 0
AuthHeaderValue = 1
)

// TykMiddleware wraps up the ApiSpec and Proxy objects to be included in a
// middleware handler, this can probably be handled better.
type TykMiddleware struct {
Spec APISpec
Proxy *httputil.ReverseProxy
}

// SuccessHandler represents the final ServeHTTP() request for a proxied API request
type SuccessHandler struct {
TykMiddleware
}

// ServeHTTP will store the request details in the analytics store if necessary and proxy the request to it's
// final destination, this is invoked by the ProxyHandler or right at the start of a request chain if the URL
// Spec states the path is Ignored
func (s SuccessHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if config.EnableAnalytics {
t := time.Now()
Expand Down
14 changes: 7 additions & 7 deletions tests/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ func TestThrottling(t *testing.T) {
t.Error("Third request returned invalid code, should 409, got: \n", third_recorder.Code)
}

newApiError := TykErrorResponse{}
json.Unmarshal([]byte(third_recorder.Body.String()), &newApiError)
newAPIError := TykErrorResponse{}
json.Unmarshal([]byte(third_recorder.Body.String()), &newAPIError)

if newApiError.Error != "Rate limit exceeded" {
if newAPIError.Error != "Rate limit exceeded" {
t.Error("Third request returned invalid message, got: \n", third_recorder.Body.String())
}
}
Expand Down Expand Up @@ -160,10 +160,10 @@ func TestQuota(t *testing.T) {
t.Error("Third request returned invalid code, should 409, got: \n", third_recorder.Code)
}

newApiError := TykErrorResponse{}
json.Unmarshal([]byte(third_recorder.Body.String()), &newApiError)
newAPIError := TykErrorResponse{}
json.Unmarshal([]byte(third_recorder.Body.String()), &newAPIError)

if newApiError.Error != "Quota exceeded" {
t.Error("Third request returned invalid message, got: \n", newApiError.Error)
if newAPIError.Error != "Quota exceeded" {
t.Error("Third request returned invalid message, got: \n", newAPIError.Error)
}
}

0 comments on commit 083eae6

Please sign in to comment.