Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new/maniphttp: added method DirectSend #84

Merged
merged 3 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions maniphttp/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
package maniphttp

import (
"context"
"crypto/tls"
"fmt"
"net/http"
"strings"

"go.aporeto.io/elemental"
"go.aporeto.io/manipulate"
"go.aporeto.io/manipulate/internal/tracing"
)

// ExtractCredentials extracts the username and password from the given manipulator.
Expand Down Expand Up @@ -96,3 +100,29 @@ func SetGlobalHeaders(manipulator manipulate.Manipulator, headers http.Header) {

m.globalHeaders = headers
}

// DirectSend allows to send direct bytes using the given manipulator.
// This is only useful in extremely particular scenario, like fuzzing.
func DirectSend(manipulator manipulate.Manipulator, mctx manipulate.Context, endpoint string, method string, body []byte) (*http.Response, error) {

m, ok := manipulator.(*httpManipulator)
if !ok {
panic("You can only pass a HTTP Manipulator to DirectSend")
}

if mctx == nil {
mctx = manipulate.NewContext(context.Background())
}

if !strings.HasPrefix(endpoint, "/") {
endpoint = "/" + endpoint
}

v := m.computeVersion(0, mctx.Version())
url := m.url + "/" + v + endpoint

sp := tracing.StartTrace(mctx, fmt.Sprintf("maniphttp.directsend"))
defer sp.Finish()

return m.send(mctx, method, url, body, sp, 0)
}
63 changes: 61 additions & 2 deletions maniphttp/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
package maniphttp

import (
"context"
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"

"go.aporeto.io/manipulate/maniptest"

. "github.com/smartystreets/goconvey/convey"
"go.aporeto.io/manipulate/maniptest"
)

func TestManiphttp_ExtractCredentials(t *testing.T) {
Expand Down Expand Up @@ -187,3 +191,58 @@ func TestManiphttp_SetGlobalHeaders(t *testing.T) {
})
})
}

func TestManiphttp_DirectSend(t *testing.T) {

Convey("Given I have a manipulator and a test server", t, func() {

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}

if string(body) != "hello" {
panic("wrong body recieved.")
}

if !strings.HasSuffix(r.RequestURI, "/toto") {
panic(fmt.Sprintf("wrong url: %s", r.RequestURI))
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, `"bonjour"`)
}))
defer ts.Close()

m, _ := New(context.Background(), ts.URL)

Convey("When I call DirectSend", func() {

resp, err := DirectSend(m, nil, "toto", http.MethodPost, []byte("hello"))

Convey("Then err should be nil", func() {
So(err, ShouldBeNil)
})

Convey("Then the response should be correct", func() {
So(resp, ShouldNotBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusCreated)
})
})
})

Convey("Given I have a non http manipulator", t, func() {

m := maniptest.NewTestManipulator()

Convey("When I call DirectSend", func() {

Convey("Then it should panic", func() {
So(func() { _, _ = DirectSend(m, nil, "", http.MethodPost, nil) }, ShouldPanicWith, "You can only pass a HTTP Manipulator to DirectSend")
})
})
})
}