Skip to content

Commit

Permalink
Merge pull request #91 from SpectoLabs/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Karolis Rusenas committed Feb 4, 2016
2 parents 0ea52c1 + 3ed891e commit f5ac731
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
38 changes: 37 additions & 1 deletion models.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,53 @@ func extractBody(resp *http.Response) (extract []byte, err error) {
return extract, nil
}

// getRequestDetails - extracts request details, reads request body so it will be empty.
func getRequestDetails(req *http.Request) (requestObj requestDetails, err error) {
if req.Body == nil {
req.Body = ioutil.NopCloser(bytes.NewBuffer([]byte("")))
}

reqBody, err := ioutil.ReadAll(req.Body)

if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"mode": "capture",
}).Error("Got error while reading request body")
return
}

requestObj = requestDetails{
Path: req.URL.Path,
Method: req.Method,
Destination: req.Host,
Scheme: req.URL.Scheme,
Query: req.URL.RawQuery,
Body: string(reqBody),
RemoteAddr: req.RemoteAddr,
Headers: req.Header,
}
return
}

// doRequest performs original request and returns response that should be returned to client and error (if there is one)
func (d *DBClient) doRequest(request *http.Request) (*http.Response, error) {

// We can't have this set. And it only contains "/pkg/net/http/" anyway
request.RequestURI = ""

if d.cfg.middleware != "" {
// middleware is provided, modifying request
var payload Payload

rd, err := getRequestDetails(request)
if err != nil {
return nil, err
}
payload.Request = rd

c := NewConstructor(request, payload)
err := c.ApplyMiddleware(d.cfg.middleware)
err = c.ApplyMiddleware(d.cfg.middleware)

if err != nil {
log.WithFields(log.Fields{
Expand Down
18 changes: 18 additions & 0 deletions models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,24 @@ func TestModifyRequest(t *testing.T) {

}

func TestModifyRequestWODestination(t *testing.T) {
// tests modify mode but uses different middleware to not supply destination
server, dbClient := testTools(201, `{'message': 'here'}`)
defer server.Close()

dbClient.cfg.middleware = "./examples/middleware/modify_response/modify_response.py"

req, err := http.NewRequest("GET", "http://very-interesting-website.com/q=123", nil)
expect(t, err, nil)

response, err := dbClient.modifyRequestResponse(req, dbClient.cfg.middleware)
expect(t, err, nil)

// response should be changed to 201
expect(t, response.StatusCode, 201)

}

func TestModifyRequestNoMiddleware(t *testing.T) {
server, dbClient := testTools(201, `{'message': 'here'}`)
defer server.Close()
Expand Down

0 comments on commit f5ac731

Please sign in to comment.