github.com/gdamore/mangos@v1.4.0/transport/ws/ws_specific_test.go (about)

     1  // Copyright 2018 The Mangos Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use file except in compliance with the License.
     5  // You may obtain a copy of the license at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ws
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"net/http"
    21  	"testing"
    22  
    23  	"nanomsg.org/go-mangos/protocol/rep"
    24  	"nanomsg.org/go-mangos/protocol/req"
    25  	//"nanomsg.org/go-mangos/test"
    26  )
    27  
    28  func TestWebsockPath(t *testing.T) {
    29  	sockReq, _ := req.NewSocket()
    30  	sockRep, _ := rep.NewSocket()
    31  	tran := NewTransport()
    32  	l, e := tran.NewListener("ws://127.0.0.1:3335/mysock", sockReq)
    33  	if e != nil {
    34  		t.Errorf("Failed new Listener: %v", e)
    35  		return
    36  	}
    37  	d, e := tran.NewDialer("ws://127.0.0.1:3335/boguspath", sockRep)
    38  	if e != nil {
    39  		t.Errorf("Failed new Dialer: %v", e)
    40  		return
    41  	}
    42  
    43  	if e = l.Listen(); e != nil {
    44  		t.Errorf("Listen failed")
    45  		return
    46  	}
    47  	defer l.Close()
    48  
    49  	p, e := d.Dial()
    50  	if p != nil {
    51  		defer p.Close()
    52  	}
    53  	if e == nil {
    54  		t.Errorf("Dial passed, when should not have!")
    55  		return
    56  	}
    57  	t.Logf("Got expected error %v", e)
    58  }
    59  
    60  var bogusstr = "THIS IS BOGUS"
    61  
    62  func bogusHandler(w http.ResponseWriter, r *http.Request) {
    63  	fmt.Fprintf(w, bogusstr)
    64  }
    65  
    66  func TestWebsockMux(t *testing.T) {
    67  	sockReq, _ := req.NewSocket()
    68  	sockRep, _ := rep.NewSocket()
    69  	tran := NewTransport()
    70  	l, e := tran.NewListener("ws://127.0.0.1:3336/mysock", sockReq)
    71  	if e != nil {
    72  		t.Errorf("Failed new Listener: %v", e)
    73  		return
    74  	}
    75  	muxi, e := l.GetOption(OptionWebSocketMux)
    76  	if e != nil {
    77  		t.Errorf("Failed get mux: %v", e)
    78  	}
    79  	mux := muxi.(*http.ServeMux)
    80  	mux.HandleFunc("/bogus", bogusHandler)
    81  	d, e := tran.NewDialer("ws://127.0.0.1:3336/bogus", sockRep)
    82  	if e != nil {
    83  		t.Errorf("Failed new Dialer: %v", e)
    84  		return
    85  	}
    86  
    87  	if e = l.Listen(); e != nil {
    88  		t.Errorf("Listen failed")
    89  		return
    90  	}
    91  	defer l.Close()
    92  
    93  	p, e := d.Dial()
    94  	if p != nil {
    95  		defer p.Close()
    96  	}
    97  	if e == nil {
    98  		t.Errorf("Dial passed, when should not have!")
    99  		return
   100  	}
   101  	t.Logf("Got expected error %v", e)
   102  
   103  	// Now let's try to use http client.
   104  	resp, err := http.Get("http://127.0.0.1:3336/bogus")
   105  
   106  	if err != nil {
   107  		t.Errorf("Get of boguspath failed: %v", err)
   108  		return
   109  	}
   110  
   111  	if resp.StatusCode != 200 {
   112  		t.Errorf("Response code wrong: %d", resp.StatusCode)
   113  		return
   114  	}
   115  
   116  	body, err := ioutil.ReadAll(resp.Body)
   117  	if err != nil {
   118  		t.Errorf("ReadAll Failed: %v", err)
   119  		return
   120  	}
   121  	if string(body) != bogusstr {
   122  		t.Errorf("Results mismatch: %s != %s", string(body), bogusstr)
   123  	}
   124  	t.Logf("Got body: %s", string(body))
   125  }
   126  
   127  // This test verifies that we can use stock http server instances with
   128  // our own websocket handler.
   129  func TestWebsockHandler(t *testing.T) {
   130  	sockReq, _ := req.NewSocket()
   131  	sockRep, _ := rep.NewSocket()
   132  	tran := NewTransport()
   133  	l, e := tran.NewListener("ws://127.0.0.1:3337/mysock", sockReq)
   134  	if e != nil {
   135  		t.Errorf("Failed new Listener: %v", e)
   136  		return
   137  	}
   138  	hi, e := l.GetOption(OptionWebSocketHandler)
   139  	if e != nil {
   140  		t.Errorf("Failed get WebSocketHandler: %v", e)
   141  	}
   142  	handler := hi.(http.Handler)
   143  
   144  	mux := http.NewServeMux()
   145  	mux.HandleFunc("/bogus", bogusHandler)
   146  	mux.Handle("/mysock", handler)
   147  
   148  	// Note that we are *counting* on this to die gracefully when our
   149  	// program exits. There appears to be no way to shutdown http
   150  	// instances gracefully.
   151  	go http.ListenAndServe("127.0.0.1:3337", mux)
   152  
   153  	d, e := tran.NewDialer("ws://127.0.0.1:3337/bogus", sockRep)
   154  	if e != nil {
   155  		t.Errorf("Failed new Dialer: %v", e)
   156  		return
   157  	}
   158  
   159  	defer l.Close()
   160  
   161  	p, e := d.Dial()
   162  	if p != nil {
   163  		defer p.Close()
   164  	}
   165  	if e == nil {
   166  		t.Errorf("Dial passed, when should not have!")
   167  		return
   168  	}
   169  	t.Logf("Got expected error %v", e)
   170  
   171  	// Now let's try to use http client.
   172  	resp, err := http.Get("http://127.0.0.1:3337/bogus")
   173  
   174  	if err != nil {
   175  		t.Errorf("Get of boguspath failed: %v", err)
   176  		return
   177  	}
   178  
   179  	if resp.StatusCode != 200 {
   180  		t.Errorf("Response code wrong: %d", resp.StatusCode)
   181  		return
   182  	}
   183  
   184  	body, err := ioutil.ReadAll(resp.Body)
   185  	if err != nil {
   186  		t.Errorf("ReadAll Failed: %v", err)
   187  		return
   188  	}
   189  	if string(body) != bogusstr {
   190  		t.Errorf("Results mismatch: %s != %s", string(body), bogusstr)
   191  	}
   192  	t.Logf("Got body: %s", string(body))
   193  }