mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-11-09 16:35:12 +00:00
httpdriver: Add Method into MockRequest
This commit is contained in:
parent
d4bfd69cf6
commit
3bb8c2b019
|
@ -11,6 +11,7 @@ import (
|
||||||
|
|
||||||
// MockRequest is a mock request. It implements the Request interface.
|
// MockRequest is a mock request. It implements the Request interface.
|
||||||
type MockRequest struct {
|
type MockRequest struct {
|
||||||
|
Method string
|
||||||
URL url.URL
|
URL url.URL
|
||||||
Header http.Header
|
Header http.Header
|
||||||
Body []byte
|
Body []byte
|
||||||
|
@ -18,9 +19,10 @@ type MockRequest struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMockRequest creates a new mock request.
|
// NewMockRequest creates a new mock request. If any of the given parameters
|
||||||
func NewMockRequest(urlStr string, header http.Header, jsonBody interface{}) *MockRequest {
|
// cause an error, the function will panic.
|
||||||
url, err := url.Parse(urlStr)
|
func NewMockRequest(method, urlstr string, header http.Header, jsonBody interface{}) *MockRequest {
|
||||||
|
u, err := url.Parse(urlstr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -34,23 +36,26 @@ func NewMockRequest(urlStr string, header http.Header, jsonBody interface{}) *Mo
|
||||||
}
|
}
|
||||||
|
|
||||||
return &MockRequest{
|
return &MockRequest{
|
||||||
URL: *url,
|
Method: method,
|
||||||
|
URL: *u,
|
||||||
Header: header,
|
Header: header,
|
||||||
Body: body,
|
Body: body,
|
||||||
ctx: context.Background(),
|
ctx: context.Background(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMockRequestWithContext creates a new mock request with context.
|
// NewMockRequestWithContext creates a new mock request with context. If any of
|
||||||
func NewMockRequestWithContext(ctx context.Context, urlStr string, header http.Header, jsonBody interface{}) *MockRequest {
|
// the given parameters cause an error, the function will panic.
|
||||||
req := NewMockRequest(urlStr, header, jsonBody)
|
func NewMockRequestWithContext(ctx context.Context, method, urlstr string, header http.Header, jsonBody interface{}) *MockRequest {
|
||||||
|
req := NewMockRequest(method, urlstr, header, jsonBody)
|
||||||
req.ctx = ctx
|
req.ctx = ctx
|
||||||
return req
|
return req
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToHTTPRequest converts a mock request to a http request.
|
// ToHTTPRequest converts a mock request to a net/http request. If an error
|
||||||
|
// occurs, the function will panic.
|
||||||
func (r *MockRequest) ToHTTPRequest() *http.Request {
|
func (r *MockRequest) ToHTTPRequest() *http.Request {
|
||||||
req, err := http.NewRequestWithContext(r.ctx, http.MethodGet, r.URL.String(), bytes.NewReader(r.Body))
|
req, err := http.NewRequestWithContext(r.ctx, r.Method, r.URL.String(), bytes.NewReader(r.Body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue