github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/rpc/dispatch_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package rpc_test
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"net/http/httptest"
    10  
    11  	jc "github.com/juju/testing/checkers"
    12  	"golang.org/x/net/websocket"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/rpc"
    16  	"github.com/juju/juju/rpc/jsoncodec"
    17  )
    18  
    19  type dispatchSuite struct {
    20  	server     *httptest.Server
    21  	serverAddr string
    22  	ready      chan struct{}
    23  }
    24  
    25  var _ = gc.Suite(&dispatchSuite{})
    26  
    27  func (s *dispatchSuite) SetUpSuite(c *gc.C) {
    28  	rpcServer := func(ws *websocket.Conn) {
    29  		codec := jsoncodec.NewWebsocket(ws)
    30  		conn := rpc.NewConn(codec, nil)
    31  
    32  		conn.Serve(&DispatchRoot{}, nil)
    33  		conn.Start()
    34  
    35  		<-conn.Dead()
    36  	}
    37  	http.Handle("/rpc", websocket.Handler(rpcServer))
    38  	s.server = httptest.NewServer(nil)
    39  	s.serverAddr = s.server.Listener.Addr().String()
    40  	s.ready = make(chan struct{}, 1)
    41  }
    42  
    43  func (s *dispatchSuite) TearDownSuite(c *gc.C) {
    44  	s.server.Close()
    45  }
    46  
    47  func (s *dispatchSuite) TestWSWithoutParams(c *gc.C) {
    48  	resp := s.request(c, `{"RequestId":1,"Type": "DispatchDummy","Id": "without","Request":"DoSomething"}`)
    49  	c.Assert(resp, gc.Equals, `{"RequestId":1,"Response":{}}`)
    50  }
    51  
    52  func (s *dispatchSuite) TestWSWithParams(c *gc.C) {
    53  	resp := s.request(c, `{"RequestId":2,"Type": "DispatchDummy","Id": "with","Request":"DoSomething", "Params": {}}`)
    54  	c.Assert(resp, gc.Equals, `{"RequestId":2,"Response":{}}`)
    55  }
    56  
    57  // request performs one request to the test server via websockets.
    58  func (s *dispatchSuite) request(c *gc.C, req string) string {
    59  	url := fmt.Sprintf("ws://%s/rpc", s.serverAddr)
    60  	ws, err := websocket.Dial(url, "", "http://localhost")
    61  	c.Assert(err, jc.ErrorIsNil)
    62  
    63  	reqdata := []byte(req)
    64  	_, err = ws.Write(reqdata)
    65  	c.Assert(err, jc.ErrorIsNil)
    66  
    67  	var resp = make([]byte, 512)
    68  	n, err := ws.Read(resp)
    69  	c.Assert(err, jc.ErrorIsNil)
    70  	resp = resp[0:n]
    71  
    72  	err = ws.Close()
    73  	c.Assert(err, jc.ErrorIsNil)
    74  
    75  	return string(resp)
    76  }
    77  
    78  // DispatchRoot simulates the root for the test.
    79  type DispatchRoot struct{}
    80  
    81  func (*DispatchRoot) DispatchDummy(id string) (*DispatchDummy, error) {
    82  	return &DispatchDummy{}, nil
    83  }
    84  
    85  // DispatchDummy is the type to whish the request is dispatched.
    86  type DispatchDummy struct{}
    87  
    88  func (d *DispatchDummy) DoSomething() {}