😀

Go の httptest のサンプル

2023/04/06に公開

概要

備忘メモφ(..)

ハンドラで何も指定していない場合はステータスコード200が返る.

サンプル

package main

import (
        "fmt"
        "net/http"
        "net/http/httptest"
)

func main() {
        mux := http.NewServeMux()
        mux.HandleFunc(
                "/hello",
                func(w http.ResponseWriter, r *http.Request) {
                },
        )
        ts := httptest.NewServer(mux)
        resp, err := http.Get(ts.URL + "/hello")
        fmt.Println(resp, err)
}

output:

&{200 OK 200 HTTP/1.1 1 1 map[Date:[Mon, 13 Apr 2015 01:10:55 GMT] Content-Length:[0] Content-Type:[text/plain; charset=utf-8]] 0xc2080441c0 0 [] false map[] 0xc208029520 <nil>} <nil>

Discussion