github.com/qiniu/x@v1.11.9/mockhttp/mockhttp_test.go (about)

     1  package mockhttp_test
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  	"net/http"
    10  	"strconv"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/qiniu/x/mockhttp"
    15  	"github.com/qiniu/x/rpc"
    16  )
    17  
    18  // --------------------------------------------------------------------
    19  
    20  func reply(w http.ResponseWriter, code int, data interface{}) {
    21  	msg, _ := json.Marshal(data)
    22  	h := w.Header()
    23  	h.Set("Content-Length", strconv.Itoa(len(msg)))
    24  	h.Set("Content-Type", "application/json")
    25  	w.WriteHeader(code)
    26  	w.Write(msg)
    27  }
    28  
    29  // --------------------------------------------------------------------
    30  
    31  type FooRet struct {
    32  	A int    `json:"a"`
    33  	B string `json:"b"`
    34  	C string `json:"c"`
    35  }
    36  
    37  type HandleRet map[string]string
    38  
    39  type FooServer struct{}
    40  
    41  func (p *FooServer) foo(w http.ResponseWriter, req *http.Request) {
    42  	reply(w, 200, &FooRet{1, req.Host, req.URL.Path})
    43  }
    44  
    45  func (p *FooServer) handle(w http.ResponseWriter, req *http.Request) {
    46  	reply(w, 200, HandleRet{"foo": "1", "bar": "2"})
    47  }
    48  
    49  func (p *FooServer) postDump(w http.ResponseWriter, req *http.Request) {
    50  	req.Body.Close()
    51  	io.Copy(w, req.Body)
    52  }
    53  
    54  func (p *FooServer) RegisterHandlers(mux *http.ServeMux) {
    55  	mux.HandleFunc("/foo", func(w http.ResponseWriter, req *http.Request) { p.foo(w, req) })
    56  	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { p.handle(w, req) })
    57  	mux.HandleFunc("/dump", func(w http.ResponseWriter, req *http.Request) { p.postDump(w, req) })
    58  }
    59  
    60  // --------------------------------------------------------------------
    61  
    62  func TestBasic(t *testing.T) {
    63  
    64  	server := new(FooServer)
    65  	server.RegisterHandlers(http.DefaultServeMux)
    66  
    67  	mockhttp.ListenAndServe("foo.com", nil)
    68  
    69  	ctx := context.TODO()
    70  	c := rpc.Client{Client: mockhttp.DefaultClient}
    71  	{
    72  		var foo FooRet
    73  		err := c.Call(ctx, &foo, "POST", "http://foo.com/foo")
    74  		if err != nil {
    75  			t.Fatal("call foo failed:", err)
    76  		}
    77  		if foo.A != 1 || foo.B != "foo.com" || foo.C != "/foo" {
    78  			t.Fatal("call foo: invalid ret")
    79  		}
    80  		fmt.Println(foo)
    81  	}
    82  	{
    83  		var ret map[string]string
    84  		err := c.Call(ctx, &ret, "POST", "http://foo.com/bar")
    85  		if err != nil {
    86  			t.Fatal("call foo failed:", err)
    87  		}
    88  		if ret["foo"] != "1" || ret["bar"] != "2" {
    89  			t.Fatal("call bar: invalid ret")
    90  		}
    91  		fmt.Println(ret)
    92  	}
    93  	{
    94  		resp, err := c.Post("http://foo.com/dump", "", nil)
    95  		if err != nil {
    96  			t.Fatal("post foo failed:", err)
    97  		}
    98  		resp.Body.Close()
    99  		resp, err = c.Post("http://foo.com/dump", "", strings.NewReader("abc"))
   100  		if err != nil {
   101  			t.Fatal("post foo failed:", err)
   102  		}
   103  		defer resp.Body.Close()
   104  		b, err := ioutil.ReadAll(resp.Body)
   105  		if err != nil {
   106  			t.Fatal("ioutil.ReadAll:", err)
   107  		}
   108  		if len(b) != 0 {
   109  			t.Fatal("body should be empty:", string(b))
   110  		}
   111  	}
   112  }
   113  
   114  // --------------------------------------------------------------------