github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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  	"github.com/juju/loggo"
    12  	jc "github.com/juju/testing/checkers"
    13  	"golang.org/x/net/websocket"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/juju/rpc"
    17  	"github.com/juju/juju/rpc/jsoncodec"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  type dispatchSuite struct {
    22  	testing.BaseSuite
    23  
    24  	server     *httptest.Server
    25  	serverAddr string
    26  	ready      chan struct{}
    27  }
    28  
    29  var _ = gc.Suite(&dispatchSuite{})
    30  
    31  func (s *dispatchSuite) SetUpSuite(c *gc.C) {
    32  	s.BaseSuite.SetUpSuite(c)
    33  	rpcServer := func(ws *websocket.Conn) {
    34  		codec := jsoncodec.NewWebsocket(ws)
    35  		conn := rpc.NewConn(codec, &notifier{})
    36  
    37  		conn.Serve(&DispatchRoot{}, nil)
    38  		conn.Start()
    39  
    40  		<-conn.Dead()
    41  	}
    42  	http.Handle("/rpc", websocket.Handler(rpcServer))
    43  	s.server = httptest.NewServer(nil)
    44  	s.serverAddr = s.server.Listener.Addr().String()
    45  	s.ready = make(chan struct{}, 1)
    46  	s.AddCleanup(func(*gc.C) {
    47  		s.server.Close()
    48  	})
    49  }
    50  
    51  func (s *dispatchSuite) SetUpTest(c *gc.C) {
    52  	s.BaseSuite.SetUpTest(c)
    53  	loggo.GetLogger("juju.rpc").SetLogLevel(loggo.TRACE)
    54  }
    55  
    56  func (s *dispatchSuite) TestWSWithoutParamsV0(c *gc.C) {
    57  	resp := s.request(c, `{"RequestId":1,"Type": "DispatchDummy","Id": "without","Request":"DoSomething"}`)
    58  	c.Assert(resp, gc.Equals, `{"RequestId":1,"Response":{}}`)
    59  }
    60  
    61  func (s *dispatchSuite) TestWSWithParamsV0(c *gc.C) {
    62  	resp := s.request(c, `{"RequestId":2,"Type": "DispatchDummy","Id": "with","Request":"DoSomething", "Params": {}}`)
    63  	c.Assert(resp, gc.Equals, `{"RequestId":2,"Response":{}}`)
    64  }
    65  
    66  func (s *dispatchSuite) TestWSWithoutParamsV1(c *gc.C) {
    67  	resp := s.request(c, `{"request-id":1,"type": "DispatchDummy","id": "without","request":"DoSomething"}`)
    68  	c.Assert(resp, gc.Equals, `{"request-id":1,"response":{}}`)
    69  }
    70  
    71  func (s *dispatchSuite) TestWSWithParamsV1(c *gc.C) {
    72  	resp := s.request(c, `{"request-id":2,"type": "DispatchDummy","id": "with","request":"DoSomething", "params": {}}`)
    73  	c.Assert(resp, gc.Equals, `{"request-id":2,"response":{}}`)
    74  }
    75  
    76  // request performs one request to the test server via websockets.
    77  func (s *dispatchSuite) request(c *gc.C, req string) string {
    78  	url := fmt.Sprintf("ws://%s/rpc", s.serverAddr)
    79  	ws, err := websocket.Dial(url, "", "http://localhost")
    80  	c.Assert(err, jc.ErrorIsNil)
    81  
    82  	reqdata := []byte(req)
    83  	_, err = ws.Write(reqdata)
    84  	c.Assert(err, jc.ErrorIsNil)
    85  
    86  	var resp = make([]byte, 512)
    87  	n, err := ws.Read(resp)
    88  	c.Assert(err, jc.ErrorIsNil)
    89  	resp = resp[0:n]
    90  
    91  	err = ws.Close()
    92  	c.Assert(err, jc.ErrorIsNil)
    93  
    94  	return string(resp)
    95  }
    96  
    97  // DispatchRoot simulates the root for the test.
    98  type DispatchRoot struct{}
    99  
   100  func (*DispatchRoot) DispatchDummy(id string) (*DispatchDummy, error) {
   101  	return &DispatchDummy{}, nil
   102  }
   103  
   104  // DispatchDummy is the type to whish the request is dispatched.
   105  type DispatchDummy struct{}
   106  
   107  func (d *DispatchDummy) DoSomething() {}