Standard library / net/http/httptest

httptest.ResponseRecorder

Captures handler status, headers, body and flush calls in memory. It does not simulate a real network connection. Expected stdout: '201 created\n'.

Formal Syntax & Structure

go syntaxgo
type ResponseRecorder struct {  Code int
  HeaderMap http.Header
  Body *bytes.Buffer
  Flushed bool
}

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http/httptest")
func main() {  w := httptest.NewRecorder()  w.WriteHeader(201)  fmt.Fprint(w, "created")  fmt.Println(w.Code, w.Body.String())}
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)  }}
Go Reference