Skip to content

Commit

Permalink
Merge pull request #5 from tarent/pass-http-status-codes
Browse files Browse the repository at this point in the history
Pass http status codes
  • Loading branch information
domano authored Jun 21, 2016
2 parents 5622b66 + 87f66fb commit 20d3c39
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composition/composition_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (agg *CompositionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

} else if res.Def.Required {
log.WithField("fetchResult", res).Errorf("error loading content from: %v", res.Def.URL)
res.Def.ErrHandler.Handle(res.Err, w, r)
res.Def.ErrHandler.Handle(res.Err, res.Content.HttpStatusCode(), w, r)
return
} else {
log.WithField("fetchResult", res).Warnf("optional content not loaded: %v", res.Def.URL)
Expand Down
4 changes: 2 additions & 2 deletions composition/composition_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func Test_CompositionHandler_ErrorInFetching(t *testing.T) {
return MockFetchResultSupplier{
&FetchResult{
Def: NewFetchDefinition("/foo"),
Content: nil,
Content: &MemoryContent{httpStatusCode: 502},
Err: errors.New(errorString),
},
}
Expand All @@ -185,7 +185,7 @@ func Test_CompositionHandler_ErrorInFetching(t *testing.T) {
r, _ := http.NewRequest("GET", "http://example.com", nil)
aggregator.ServeHTTP(resp, r)

a.Equal("Bad Gateway: "+errorString+"\n", string(resp.Body.Bytes()))
a.Equal("Error: "+errorString+"\n", string(resp.Body.Bytes()))
a.Equal(502, resp.Code)
}

Expand Down
4 changes: 2 additions & 2 deletions composition/fetch_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,6 @@ func NewDefaultErrorHandler() *DefaultErrorHandler {
return deh
}

func (der *DefaultErrorHandler) Handle(err error, w http.ResponseWriter, r *http.Request) {
http.Error(w, "Bad Gateway: "+err.Error(), 502)
func (der *DefaultErrorHandler) Handle(err error, status int, w http.ResponseWriter, r *http.Request) {
http.Error(w, "Error: "+err.Error(), status)
}
2 changes: 1 addition & 1 deletion composition/fetch_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ func Test_FetchDefinition_use_DefaultErrorHandler_if_not_set(t *testing.T) {
a := assert.New(t)

fd := NewFetchDefinitionWithErrorHandler("http://upstream:8080/", nil)
a.Equal(NewDefaultErrorHandler(), fd.ErrHandler)
a.Equal(NewDefaultErrorHandler(), fd.ErrHandler)
}
21 changes: 13 additions & 8 deletions composition/http_content_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ func NewHttpContentLoader() *HttpContentLoader {
func (loader *HttpContentLoader) Load(fd *FetchDefinition) (Content, error) {
client := &http.Client{Timeout: fd.Timeout}

c := NewMemoryContent()
c.url = fd.URL
c.httpStatusCode = 502

var err error
request, err := http.NewRequest(fd.Method, fd.URL, fd.Body)
if err != nil {
return nil, err
return c, err
}
request.Header = fd.Header
if request.Header == nil {
Expand All @@ -41,26 +45,27 @@ func (loader *HttpContentLoader) Load(fd *FetchDefinition) (Content, error) {

resp, err := client.Do(request)

if resp != nil {
c.httpStatusCode = resp.StatusCode
}

logging.Call(request, resp, start, err)
if err != nil {
return nil, err
return c, err
}

if fd.RespProc != nil {
err = fd.RespProc.Process(resp, fd.URL)
}
if err != nil {
return nil, err
return c, err
}

if resp.StatusCode < 200 || resp.StatusCode > 399 {
return nil, fmt.Errorf("(http %v) on loading url %q", resp.StatusCode, fd.URL)
if c.httpStatusCode < 200 || c.httpStatusCode > 399 {
return c, fmt.Errorf("(http %v) on loading url %q", c.httpStatusCode, fd.URL)
}

c := NewMemoryContent()
c.url = fd.URL
c.httpHeader = resp.Header
c.httpStatusCode = resp.StatusCode

// take the first parser for the content type
// direct access to the map does not work, because the
Expand Down
21 changes: 18 additions & 3 deletions composition/http_content_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ func Test_HttpContentLoader_LoadStream_No_Composition_Header(t *testing.T) {
a.Equal("{}", string(body))
}

func Test_HttpContentLoader_Pass_404(t *testing.T) {
a := assert.New(t)

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
w.Write([]byte("{}"))
}))

defer server.Close()

loader := &HttpContentLoader{}
c, err := loader.Load(NewFetchDefinition(server.URL))
a.Error(err)
a.Equal(404, c.HttpStatusCode())
}

func Test_HttpContentLoader_LoadError500(t *testing.T) {
a := assert.New(t)

Expand All @@ -162,17 +178,16 @@ func Test_HttpContentLoader_LoadError500(t *testing.T) {
loader := &HttpContentLoader{}
c, err := loader.Load(NewFetchDefinition(server.URL))
a.Error(err)
a.Nil(c)
a.Contains(err.Error(), "http 500")
assert.True(t, c.HttpStatusCode() == 500)
}

func Test_HttpContentLoader_LoadErrorNetwork(t *testing.T) {
a := assert.New(t)

loader := &HttpContentLoader{}
c, err := loader.Load(NewFetchDefinition("..."))
_, err := loader.Load(NewFetchDefinition("..."))
a.Error(err)
a.Nil(c)
a.Contains(err.Error(), "unsupported protocol scheme")
}

Expand Down
4 changes: 2 additions & 2 deletions composition/interface_mocks_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Automatically generated by MockGen. DO NOT EDIT!
// Source: github.com/tarent/lib-compose/composition (interfaces: Fragment,ContentLoader,Content,ContentMerger,ContentParser,ResponseProcessor)
// Source: lib-compose/composition (interfaces: Fragment,ContentLoader,Content,ContentMerger,ContentParser,ResponseProcessor)

package composition

import (
gomock "github.com/golang/mock/gomock"

io "io"

http "net/http"
)

Expand Down
8 changes: 4 additions & 4 deletions composition/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package composition

//go:generate go get github.com/golang/mock/mockgen
//go:generate mockgen -self_package composition -package composition -destination interface_mocks_test.go github.com/tarent/lib-compose/composition Fragment,ContentLoader,Content,ContentMerger,ContentParser,ResponseProcessor
//go:generate sed -ie "s/composition .github.com\\/tarent\\/lib-compose\\/composition.//g;s/composition\\.//g" interface_mocks_test.go
//go:generate mockgen -self_package composition -package composition -destination interface_mocks_test.go lib-compose/composition Fragment,ContentLoader,Content,ContentMerger,ContentParser,ResponseProcessor
//go:generate sed -ie "s/composition .lib-compose\\/composition.//g;s/composition\\.//g" interface_mocks_test.go
import (
"io"
"net/http"
Expand All @@ -15,7 +15,7 @@ type Fragment interface {
type ContentLoader interface {
// Load synchronously loads a content.
// The loader has to ensure to return the call withing the supplied timeout.
Load(fd *FetchDefinition) (Content, error)
Load(fd *FetchDefinition) (content Content, err error)
}

type ContentParser interface {
Expand Down Expand Up @@ -87,5 +87,5 @@ type ResponseProcessor interface {

type ErrorHandler interface {
// handle http request errors
Handle(err error, w http.ResponseWriter, r *http.Request)
Handle(err error, status int, w http.ResponseWriter, r *http.Request)
}
2 changes: 1 addition & 1 deletion composition_example/example_ui_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
gorilla "github.com/gorilla/handlers"
"github.com/tarent/lib-compose/composition"
"net/http"
"os"
"github.com/tarent/lib-compose/composition"
)

func main() {
Expand Down

0 comments on commit 20d3c39

Please sign in to comment.