Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>httptest does two types of tests: response and server</p> <p>Response test:</p> <pre><code>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) } } } </code></pre> <p>Server test (which is what you need to use):</p> <pre><code>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 := &lt;-c if tweet != expected1 { t.Fail() } tweet = &lt;-c if tweet != expected2 { t.Fail() } } </code></pre> <p>BTW, you don't need to pass in the pointer of r, because it's already a pointer. </p> <pre><code>err = json.Unmarshal(body, r) </code></pre> <p>EDIT: for my recorder test, I could use my http handler like this:</p> <pre><code>handler(resp, req) </code></pre> <p>But my original code is not using the default mux (but from Gorilla/mux), and I have some wrapping around the mux, e.g. insert server logging, and adding request context (Gorilla/context), so I had to start from mux and call ServeHTTP</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload