github.com/cozy/cozy-stack@v0.0.0-20240327093429-939e4a21320e/web/server_test.go (about)

     1  package web
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"net/http"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/labstack/echo/v4"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestServers_ok(t *testing.T) {
    17  	major := echo.New()
    18  	major.HideBanner = true
    19  	major.HidePort = true
    20  	major.GET("/ping", func(e echo.Context) error {
    21  		return e.Blob(http.StatusOK, "application/text", []byte("pong major"))
    22  	})
    23  
    24  	servers := NewServers()
    25  	defer servers.Shutdown(context.Background())
    26  
    27  	err := servers.Start(major, "major", "localhost:")
    28  	require.NoError(t, err)
    29  
    30  	res, err := http.Get(fmt.Sprintf("http://%s/ping", servers.GetAddr("major")))
    31  	require.NoError(t, err)
    32  	defer res.Body.Close()
    33  
    34  	raw, err := io.ReadAll(res.Body)
    35  	require.NoError(t, err)
    36  
    37  	assert.Equal(t, "pong major", string(raw))
    38  }
    39  
    40  func TestServers_handle_ipv4_ipv6_on_localhost(t *testing.T) {
    41  	major := echo.New()
    42  	major.HideBanner = true
    43  	major.HidePort = true
    44  	major.GET("/ping", func(e echo.Context) error {
    45  		return e.Blob(http.StatusOK, "application/text", []byte("pong major"))
    46  	})
    47  
    48  	servers := NewServers()
    49  	defer servers.Shutdown(context.Background())
    50  
    51  	err := servers.Start(major, "major", "localhost:38423")
    52  	require.NoError(t, err)
    53  
    54  	// Need some time to start the goroutines.
    55  	time.Sleep(50 * time.Millisecond)
    56  
    57  	// Call the host on IPv4
    58  	res, err := http.Get("http://127.0.0.1:38423/ping")
    59  	require.NoError(t, err)
    60  
    61  	raw, err := io.ReadAll(res.Body)
    62  	require.NoError(t, err)
    63  
    64  	assert.Equal(t, "pong major", string(raw))
    65  
    66  	// Call the host on IPv6
    67  	res, err = http.Get("http://::1:38423/ping")
    68  	require.NoError(t, err)
    69  	defer res.Body.Close()
    70  
    71  	raw, err = io.ReadAll(res.Body)
    72  	require.NoError(t, err)
    73  
    74  	assert.Equal(t, "pong major", string(raw))
    75  }
    76  
    77  func TestServers_with_a_missing_port(t *testing.T) {
    78  	major := echo.New()
    79  	major.HideBanner = true
    80  	major.HidePort = true
    81  	major.GET("/ping", func(e echo.Context) error {
    82  		return e.Blob(http.StatusOK, "application/text", []byte("pong major"))
    83  	})
    84  
    85  	servers := NewServers()
    86  	defer servers.Shutdown(context.Background())
    87  
    88  	err := servers.Start(major, "major", "localhost")
    89  	assert.EqualError(t, err, "address localhost: missing port in address")
    90  }
    91  
    92  func TestServers_with_an_invalid_host(t *testing.T) {
    93  	major := echo.New()
    94  	major.HideBanner = true
    95  	major.HidePort = true
    96  	major.GET("/ping", func(e echo.Context) error {
    97  		return e.Blob(http.StatusOK, "application/text", []byte("pong major"))
    98  	})
    99  
   100  	servers := NewServers()
   101  	defer servers.Shutdown(context.Background())
   102  
   103  	err := servers.Start(major, "major", "[[localhost:32")
   104  	assert.EqualError(t, err, "address [[localhost:32: missing ']' in address")
   105  }
   106  
   107  func TestServers_with_an_missing_argument(t *testing.T) {
   108  	major := echo.New()
   109  	major.HideBanner = true
   110  	major.HidePort = true
   111  	major.GET("/ping", func(e echo.Context) error {
   112  		return e.Blob(http.StatusOK, "application/text", []byte("pong major"))
   113  	})
   114  
   115  	servers := NewServers()
   116  	defer servers.Shutdown(context.Background())
   117  
   118  	err := servers.Start(major, "", "localhost:432")
   119  	assert.ErrorIs(t, err, ErrMissingArgument)
   120  
   121  	err = servers.Start(major, "major", "")
   122  	assert.ErrorIs(t, err, ErrMissingArgument)
   123  }