Skip to content

Commit

Permalink
Merge pull request #12 from Simversity/v3
Browse files Browse the repository at this point in the history
Testing should not follow redirects
  • Loading branch information
meson10 committed Jun 28, 2015
2 parents 8c54935 + dd4d7b7 commit 3f9101e
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions tests/mock_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package tests

import (
"bytes"
"errors"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -16,6 +16,7 @@ type MockResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data"`
Error error
}

type MockServer struct {
Expand All @@ -33,6 +34,8 @@ func (self *MockServer) Close() {
}

func (self *MockServer) Test(request *MockRequest, equality func(response *MockResponse)) {
var msg = new(MockResponse)

if request.Method == "" {
request.Method = "GET"
}
Expand All @@ -44,22 +47,38 @@ func (self *MockServer) Test(request *MockRequest, equality func(response *MockR
)

if err != nil {
log.Panic(err)
msg.Error = err
equality(msg)
return
}

req.Header.Add("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return errors.New(req.URL.String())
},
}

resp, err := client.Do(req)
if err != nil {
msg.Error = err
msg.Message = err.Error()
equality(msg)
return
}

data, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Panic(err)
msg.Error = err
equality(msg)
return
}

var msg = new(MockResponse)
utils.Decoder(data, msg)

equality(msg)
return
}

func NewServer() *MockServer {
Expand Down

0 comments on commit 3f9101e

Please sign in to comment.