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