如何使用httptest测试http调用

jwe*_*nga 65 http go

我有以下代码:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "time"
)

type twitterResult struct {
    Results []struct {
        Text     string `json:"text"`
        Ids      string `json:"id_str"`
        Name     string `json:"from_user_name"`
        Username string `json:"from_user"`
        UserId   string `json:"from_user_id_str"`
    }
}

var (
  twitterUrl = "http://search.twitter.com/search.json?q=%23UCL"
  pauseDuration = 5 * time.Second
)

func retrieveTweets(c chan<- *twitterResult) {
    for {
        resp, err := http.Get(twitterUrl)
        if err != nil {
            log.Fatal(err)
        }

        defer resp.Body.Close()
        body, err := ioutil.ReadAll(resp.Body)
        r := new(twitterResult) //or &twitterResult{} which returns *twitterResult
        err = json.Unmarshal(body, &r)
        if err != nil {
            log.Fatal(err)
        }
        c <- r
        time.Sleep(pauseDuration)
    }

}

func displayTweets(c chan *twitterResult) {
    tweets := <-c
    for _, v := range tweets.Results {
        fmt.Printf("%v:%v\n", v.Username, v.Text)
    }

}

func main() {
    c := make(chan *twitterResult)
    go retrieveTweets(c)
    for {
        displayTweets(c)
    }

}
Run Code Online (Sandbox Code Playgroud)

我想为它编写一些测试,但我不确定如何使用httptest包http://golang.org/pkg/net/http/httptest/会很感激一些指针

我想出了这个(从测试中无耻地复制到了OAuth https://code.google.com/p/goauth2/source/browse/oauth/oauth_test.go):

var request = struct {
    path, query       string // request
    contenttype, body string // response
}{
    path:        "/search.json?",
    query:       "q=%23Kenya",
    contenttype: "application/json",
    body:        twitterResponse,
}

var (
    twitterResponse = `{ 'results': [{'text':'hello','id_str':'34455w4','from_user_name':'bob','from_user_id_str':'345424'}]}`
)

func TestRetrieveTweets(t *testing.T) {
    handler := func(w http.ResponseWriter, r *http.Request) {

        w.Header().Set("Content-Type", request.contenttype)
        io.WriteString(w, request.body)
    }

    server := httptest.NewServer(http.HandlerFunc(handler))
    defer server.Close()

    resp, err := http.Get(server.URL)
    if err != nil {
        t.Fatalf("Get: %v", err)
    }
    checkBody(t, resp, twitterResponse)
}

func checkBody(t *testing.T, r *http.Response, body string) {
    b, err := ioutil.ReadAll(r.Body)
    if err != nil {
        t.Error("reading reponse body: %v, want %q", err, body)
    }
    if g, w := string(b), body; g != w {
        t.Errorf("request body mismatch: got %q, want %q", g, w)
    }
}
Run Code Online (Sandbox Code Playgroud)

nem*_*emo 80

httptest有两种类型的测试:响应和服务器

反应测试:

func TestHeader3D(t *testing.T) {
    resp := httptest.NewRecorder()

    uri := "/3D/header/?"
    path := "/home/test"
    unlno := "997225821"

    param := make(url.Values)
    param["param1"] = []string{path}
    param["param2"] = []string{unlno}

    req, err := http.NewRequest("GET", uri+param.Encode(), nil)
    if err != nil {
            t.Fatal(err)
    }

    http.DefaultServeMux.ServeHTTP(resp, req)
    if p, err := ioutil.ReadAll(resp.Body); err != nil {
            t.Fail()
    } else {
            if strings.Contains(string(p), "Error") {
                    t.Errorf("header response shouldn't return error: %s", p)
            } else if !strings.Contains(string(p), `expected result`) {
                    t.Errorf("header response doen't match:\n%s", p)
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

服务器测试(这是你需要使用的):

func TestIt(t *testing.T){
    ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")
        fmt.Fprintln(w, `{"fake twitter json string"}`)
    }))
    defer ts.Close()

    twitterUrl = ts.URL
    c := make(chan *twitterResult)
    go retrieveTweets(c)

    tweet := <-c
    if tweet != expected1 {
        t.Fail()
    }
    tweet = <-c
    if tweet != expected2 {
        t.Fail()
    }
}
Run Code Online (Sandbox Code Playgroud)

顺便说一下,你不需要传入r的指针,因为它已经是一个指针.

err = json.Unmarshal(body, r)
Run Code Online (Sandbox Code Playgroud)

编辑:对于我的录音机测试,我可以像这样使用我的http处理程序:

handler(resp, req)
Run Code Online (Sandbox Code Playgroud)

但我原来的代码不使用默认的复用(但是从大猩猩/ MUX),和我身边有复用,如刀片服务器日志记录了一些包装,并添加请求上下文(大猩猩/上下文),所以我不得不从MUX开始,致电ServeHTTP

  • 请添加完整代码,例如导入所有包.新手在这里;)谢谢! (11认同)
  • 你可以使用goimports (5认同)

Jam*_*dge 7

如果您想测试您的程序,通常最好在考虑测试的情况下编写它.例如,如果您将retrieveTweets函数的内部循环解压缩为:

func downloadTweets(tweetsUrl string) (*twitterResult, error)
Run Code Online (Sandbox Code Playgroud)

您可以使用您使用该httptest软件包设置的测试服务器的URL调用它,而不必担心睡眠或重复请求.

  • `httptest`软件包是用于建立用于测试的小型HTTP服务器的基础结构。您可以以通常的方式实现请求处理程序,然后在该服务器而不是Twitter上运行代码。 (4认同)