-
-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package fasthttpexpect | ||
|
||
import ( | ||
"github.com/valyala/fasthttp" | ||
"net/http" | ||
) | ||
|
||
// Binder implements networkless httpexpect.Client attached directly to | ||
// fasthttp.RequestHandler. | ||
type Binder struct { | ||
handler fasthttp.RequestHandler | ||
} | ||
|
||
// NewBinder returns a new Binder given fasthttp.RequestHandler. | ||
func NewBinder(handler fasthttp.RequestHandler) *Binder { | ||
return &Binder{handler} | ||
} | ||
|
||
// Do implements httpexpect.Client.Do. | ||
func (binder *Binder) Do(stdreq *http.Request) (*http.Response, error) { | ||
var fastreq fasthttp.Request | ||
|
||
convertRequest(stdreq, &fastreq) | ||
|
||
var ctx fasthttp.RequestCtx | ||
|
||
ctx.Init(&fastreq, nil, nil) | ||
|
||
if stdreq.Body != nil { | ||
ctx.Request.SetBodyStream(stdreq.Body, -1) | ||
} | ||
|
||
binder.handler(&ctx) | ||
|
||
return convertResponse(stdreq, &ctx.Response), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package fasthttpexpect | ||
|
||
import ( | ||
"bytes" | ||
"github.com/valyala/fasthttp" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
func convertRequest(stdreq *http.Request, fastreq *fasthttp.Request) { | ||
if stdreq.Body != nil { | ||
fastreq.SetBodyStream(stdreq.Body, -1) | ||
} | ||
|
||
fastreq.SetRequestURI(stdreq.URL.String()) | ||
|
||
fastreq.Header.SetMethod(stdreq.Method) | ||
|
||
for k, a := range stdreq.Header { | ||
for _, v := range a { | ||
fastreq.Header.Add(k, v) | ||
} | ||
} | ||
} | ||
|
||
func convertResponse(stdreq *http.Request, fastresp *fasthttp.Response) *http.Response { | ||
status := fastresp.Header.StatusCode() | ||
body := fastresp.Body() | ||
|
||
stdresp := &http.Response{ | ||
Request: stdreq, | ||
StatusCode: status, | ||
Status: http.StatusText(status), | ||
} | ||
|
||
fastresp.Header.VisitAll(func(k, v []byte) { | ||
if stdresp.Header == nil { | ||
stdresp.Header = make(http.Header) | ||
} | ||
stdresp.Header.Add(string(k), string(v)) | ||
}) | ||
|
||
if body != nil { | ||
stdresp.Body = readCloserAdapter{bytes.NewReader(body)} | ||
} | ||
|
||
return stdresp | ||
} | ||
|
||
type readCloserAdapter struct { | ||
io.Reader | ||
} | ||
|
||
func (b readCloserAdapter) Close() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package fasthttpexpect provides fasthttp adapter for httpexpect. | ||
package fasthttpexpect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters