nanomsg.org/go/mangos/v2@v2.0.9-0.20200203084354-8a092611e461/transport/ws/ws_specific_test.go (about)

     1  // Copyright 2019 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  	"time"
    23  
    24  	"nanomsg.org/go/mangos/v2/protocol/rep"
    25  	"nanomsg.org/go/mangos/v2/protocol/req"
    26  	// "nanomsg.org/go-mangos/test"
    27  )
    28  
    29  func TestWebsockPath(t *testing.T) {
    30  	sockReq, _ := req.NewSocket()
    31  	sockRep, _ := rep.NewSocket()
    32  	tran := Transport
    33  	l, e := tran.NewListener("ws://127.0.0.1:3335/mysock", sockReq)
    34  	if e != nil {
    35  		t.Errorf("Failed new Listener: %v", e)
    36  		return
    37  	}
    38  	d, e := tran.NewDialer("ws://127.0.0.1:3335/boguspath", sockRep)
    39  	if e != nil {
    40  		t.Errorf("Failed new Dialer: %v", e)
    41  		return
    42  	}
    43  
    44  	if e = l.Listen(); e != nil {
    45  		t.Errorf("Listen failed")
    46  		return
    47  	}
    48  	defer func() {
    49  		_ = l.Close()
    50  	}()
    51  	p, e := d.Dial()
    52  	if p != nil {
    53  		defer func() {
    54  			_ = p.Close()
    55  		}()
    56  	}
    57  	if e == nil {
    58  		t.Errorf("Dial passed, when should not have!")
    59  		return
    60  	}
    61  }
    62  
    63  var bogusstr = "THIS IS BOGUS"
    64  
    65  func bogusHandler(w http.ResponseWriter, _ *http.Request) {
    66  	_, _ = fmt.Fprint(w, bogusstr)
    67  }
    68  
    69  func TestWebsockMux(t *testing.T) {
    70  	sockReq, _ := req.NewSocket()
    71  	sockRep, _ := rep.NewSocket()
    72  	tran := Transport
    73  	l, e := tran.NewListener("ws://127.0.0.1:3336/mysock", sockReq)
    74  	if e != nil {
    75  		t.Errorf("Failed new Listener: %v", e)
    76  		return
    77  	}
    78  	muxi, e := l.GetOption(OptionWebSocketMux)
    79  	if e != nil {
    80  		t.Errorf("Failed get mux: %v", e)
    81  	}
    82  	mux := muxi.(*http.ServeMux)
    83  	mux.HandleFunc("/bogus", bogusHandler)
    84  	d, e := tran.NewDialer("ws://127.0.0.1:3336/bogus", sockRep)
    85  	if e != nil {
    86  		t.Errorf("Failed new Dialer: %v", e)
    87  		return
    88  	}
    89  
    90  	if e = l.Listen(); e != nil {
    91  		t.Errorf("Listen failed")
    92  		return
    93  	}
    94  	defer func() {
    95  		_ = l.Close()
    96  	}()
    97  
    98  	p, e := d.Dial()
    99  	if p != nil {
   100  		defer func() {
   101  			_ = p.Close()
   102  		}()
   103  	}
   104  	if e == nil {
   105  		t.Errorf("Dial passed, when should not have!")
   106  		return
   107  	}
   108  
   109  	// Now let's try to use http client.
   110  	resp, err := http.Get("http://127.0.0.1:3336/bogus")
   111  
   112  	if err != nil {
   113  		t.Errorf("Get of boguspath failed: %v", err)
   114  		return
   115  	}
   116  
   117  	if resp.StatusCode != 200 {
   118  		t.Errorf("Response code wrong: %d", resp.StatusCode)
   119  		return
   120  	}
   121  
   122  	body, err := ioutil.ReadAll(resp.Body)
   123  	if err != nil {
   124  		t.Errorf("ReadAll Failed: %v", err)
   125  		return
   126  	}
   127  	if string(body) != bogusstr {
   128  		t.Errorf("Results mismatch: %s != %s", string(body), bogusstr)
   129  	}
   130  }
   131  
   132  // This test verifies that we can use stock http server instances with
   133  // our own websocket handler.
   134  func TestWebsockHandler(t *testing.T) {
   135  	sockReq, _ := req.NewSocket()
   136  	sockRep, _ := rep.NewSocket()
   137  	tran := Transport
   138  	l, e := tran.NewListener("ws://127.0.0.1:3337/mysock", sockReq)
   139  	if e != nil {
   140  		t.Errorf("Failed new Listener: %v", e)
   141  		return
   142  	}
   143  	hi, e := l.GetOption(OptionWebSocketHandler)
   144  	if e != nil {
   145  		t.Errorf("Failed get WebSocketHandler: %v", e)
   146  	}
   147  	handler := hi.(http.Handler)
   148  
   149  	mux := http.NewServeMux()
   150  	mux.HandleFunc("/bogus", bogusHandler)
   151  	mux.Handle("/mysock", handler)
   152  
   153  	// Note that we are *counting* on this to die gracefully when our
   154  	// program exits. There appears to be no way to shutdown http
   155  	// instances gracefully.
   156  	go func() {
   157  		_ = http.ListenAndServe("127.0.0.1:3337", mux)
   158  	}()
   159  
   160  	// Give the server a chance to startup, as we are running it asynch
   161  	time.Sleep(time.Second / 10)
   162  
   163  	d, e := tran.NewDialer("ws://127.0.0.1:3337/bogus", sockRep)
   164  	if e != nil {
   165  		t.Errorf("Failed new Dialer: %v", e)
   166  		return
   167  	}
   168  
   169  	defer func() {
   170  		_ = l.Close()
   171  	}()
   172  
   173  	p, e := d.Dial()
   174  	if p != nil {
   175  		defer func() {
   176  			_ = p.Close()
   177  		}()
   178  	}
   179  	if e == nil {
   180  		t.Errorf("Dial passed, when should not have!")
   181  		return
   182  	}
   183  
   184  	// Now let's try to use http client.
   185  	resp, err := http.Get("http://127.0.0.1:3337/bogus")
   186  
   187  	if err != nil {
   188  		t.Errorf("Get of boguspath failed: %v", err)
   189  		return
   190  	}
   191  
   192  	if resp.StatusCode != 200 {
   193  		t.Errorf("Response code wrong: %d", resp.StatusCode)
   194  		return
   195  	}
   196  
   197  	body, err := ioutil.ReadAll(resp.Body)
   198  	if err != nil {
   199  		t.Errorf("ReadAll Failed: %v", err)
   200  		return
   201  	}
   202  	if string(body) != bogusstr {
   203  		t.Errorf("Results mismatch: %s != %s", string(body), bogusstr)
   204  	}
   205  }