Standard library / net/http/httptest
httptest.NewRequest()
Creates an incoming server request for handler tests; nil body is allowed, invalid targets panic. Expected stdout: 'POST /demo 2\n'.
Formal Syntax & Structure
go syntaxgo
func NewRequest(method, target string, body io.Reader) *http.RequestParameters & Attributes
| Name | Type | Description |
|---|---|---|
| method | string | HTTP method token. |
| target | string | Target error to match. |
| body | io.Reader | Optional reader containing the request body. |
Return Value
*http.Request
Operation result; check errors and validity indicators when returned.
Working Example
Example Snippetgo
package main
import ( "fmt" "net/http/httptest" "strings")
func main() { r := httptest.NewRequest("POST", "/demo", strings.NewReader("hi")) fmt.Println(r.Method, r.URL.Path, r.ContentLength)}
func must[T any](v T, e error) T { if e != nil { panic(e) } return v}func check(e error) { if e != nil { panic(e) }}