Documentation ¶
Overview ¶
Package httptask provides some helper to wrap http server and some client job into task.
Index ¶
- func Server(s *http.Server, shutdownMods ...task.CtxMod) task.Task
- func ToBody[T io.ReadCloser](g forge.Generator[T]) forge.Generator[io.ReadCloser]
- type ReqGen
- func (r ReqGen) AddCookie(c *http.Cookie) ReqGen
- func (r ReqGen) AddHeader(k, v string) ReqGen
- func (r ReqGen) Body(bodyGen forge.Generator[io.ReadCloser]) ReqGen
- func (r ReqGen) ContentType(typ string) ReqGen
- func (r ReqGen) Customize(f func(*http.Request) (*http.Request, error)) ReqGen
- func (r ReqGen) Do() forge.Generator[*http.Response]
- func (r ReqGen) DoWith(cl *http.Client) forge.Generator[*http.Response]
- func (r ReqGen) GetBody(bodyGen forge.Generator[io.ReadCloser]) ReqGen
- func (r ReqGen) Location(locGen forge.Generator[string]) ReqGen
- func (r ReqGen) SetHeader(k, v string) ReqGen
- func (r ReqGen) URL(urlGen forge.Generator[*url.URL]) ReqGen
- func (r ReqGen) URLEncoded() ReqGen
- func (r ReqGen) Update(f func(*http.Request) *http.Request) ReqGen
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Server ¶
Server wraps s into a task so it can shutdown gracefully when canceled.
s.Shutdown is called with a new context modified by shutdownMods.
func ToBody ¶ added in v0.2.0
func ToBody[T io.ReadCloser](g forge.Generator[T]) forge.Generator[io.ReadCloser]
ToBody is a dirty hack to fix generic type error when you set body.
Types ¶
type ReqGen ¶
ReqGen is a forge.Generator which generates HTTP request.
func NewRequest ¶
NewRequest wraps http.NewRequestWithContext into a ReqGen.
It generates a request with empty body by default, use ReqGen.Body or ReqGen.GetBody to set a body. You might want to take a look at forge.StringReader, forge.BytesReader, forge.OpenFile and forge.FsFile to save your life.
func (ReqGen) Body ¶ added in v0.2.0
Body sets the request body to request.
If a type error strikes you, give following code a try:
r.Body(forge.ToBody(bodyGen))
If you want to retry failed http request, you might want to cache the body with forge.Cached to prevent, for example, re-openning same file or generating new bytes.Buffer from same content.
func (ReqGen) ContentType ¶ added in v0.2.0
ContentType is a shortcut so set Content-Type header.
func (ReqGen) Do ¶
Do is shortcut to DoWith(nil)
Timeout info can be set without changing http client (using context). But when to set timeout info is critical. Take a look at following two examples.
Example (Correct) ¶
globalCtx, stop := context.WithCancel(context.TODO()) defer stop() // Start a http server at http://127.0.0.1:9487 for test // // It provides an endpoint "/slow" which will write response body after // waiting 100ms. done := newServer().Go(globalCtx) time.Sleep(10 * time.Millisecond) extractBody := func(r *http.Response) ([]byte, error) { defer r.Body.Close() return io.ReadAll(r.Body) } req := NewRequest( http.MethodGet, "http://127.0.0.1:9487/slow", ).Do() // The timeout info covers full transportation of data, including request // sending, http header and body retrieving. buf, err := forge.Convert( req, extractBody, ).With(task.Timeout(time.Minute)).Run(globalCtx) if err != nil { fmt.Println("unexpected error:", err) return } // simulates some processing work or network latency time.Sleep(200 * time.Millisecond) // The timeout info above covers full transportation of data, including // request sending, response header and body retrieving. fmt.Println(err) fmt.Println(len(buf)) // stop http server stop() <-done
Output: <nil> 1048576
Example (Incorrect) ¶
globalCtx, stop := context.WithCancel(context.TODO()) defer stop() // Start a http server at http://127.0.0.1:9487 for test // // It provides an endpoint "/slow" which will write response body after // waiting 100ms. done := newServer().Go(globalCtx) time.Sleep(10 * time.Millisecond) resp, err := NewRequest( http.MethodGet, "http://127.0.0.1:9487/slow", ).Do().With(task.Timeout(time.Minute)).Run(globalCtx) if err != nil { fmt.Println("unexpected error:", err) return } defer resp.Body.Close() // simulates some processing work or network latency time.Sleep(200 * time.Millisecond) // The timeout info above covers only request sending and response header // retrieving, so context is canceled now. // now read body after context is canceled, causing error _, err = io.ReadAll(resp.Body) fmt.Println(errors.Is(err, context.Canceled)) // stop http server stop() <-done
Output: true
func (ReqGen) DoWith ¶
DoWith creates a forge.Generator that generates http.Response.
It builds http request using r and send the request using cl to get response.
Like idiom of http package, pass nil to cl will use http.DefaultClient, or you might use ReqGen.Do for lesser key strokes.
Take a look at ReqGen.Do for more detailed explaination and common gotcha.
func (ReqGen) GetBody ¶ added in v0.2.0
GetBody sets the request body and http.Request.GetBody to request.
If a type error strikes you, give following code a try:
r.GetBody(forge.ToBody(bodyGen))
func (ReqGen) URLEncoded ¶ added in v0.2.0
URLEncoded is a shortcut so set Content-Type header.