SYM's Tech Knowledge Index & Creation Records

「INPUT:OUTPUT=1:1以上」を掲げ構築する Tech Knowledge Stack and Index. by SYM@設計者足るため孤軍奮闘する IT Engineer.

Golang:API 実行 と httptest

GolangAPI 実行 と httptest

httptest

テスト用のモックサーバをたてることができる

  • API 実行コード
const apiurl = "https:/xxxxxx"

func buildGetRequest(name string) (*http.Request, error) {
    url := apiurl + name
    req, err := http.NewRequest(http.MethodGet, url, nil)
    if err != nil {
        return nil, err
    }
    return req, err
}

func getResource(req *http.Request) ([]byte, error) {
    client := &http.Client{Timeout: 10 * time.Second}

    resp, err := client.Do(req)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return nil, err
    }

    if resp.StatusCode != http.StatusOK {
        return nil, fmt.Errorf("Error Response. Status:%s, Body:%s", resp.Status, body)
    }

    return body, err
}
  • テストコード
func TestFailureToGet(t *testing.T) {
    ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusBadRequest)
        resp := make(map[string]string)
        resp["message"] = "Bad Request"
        jsonResp, err := json.Marshal(resp)
        if err != nil {
            log.Fatalf("Error happened in JSON marshal. Err: %s", err)
        }
        w.Write(jsonResp)
    }))
    defer ts.Close()

    req, err := http.NewRequest(http.MethodGet, ts.URL, strings.NewReader(""))
    if err != nil {
        assert.FailNow(t, "failed to build request")
    }
    actual, err := getResource(req)
    assert.EqualError(t, err, "Error Response. Status:400 Bad Request, Body:{\"message\":\"Bad Request\"}")
    assert.Nil(t, actual)
}

このように、正常系だけでなく、エラーレスポンスを返却させることもできる。(テスト捗る)

refs

Go の test を理解する - httptest サブパッケージ編

Go のテストに入門してみよう!