github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/ui/server_test.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package ui
    19  
    20  import (
    21  	"net/http"
    22  	"os"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/mysteriumnetwork/node/ui/versionmanager"
    27  
    28  	"github.com/mysteriumnetwork/node/requests"
    29  	"github.com/stretchr/testify/assert"
    30  	"golang.org/x/net/html"
    31  )
    32  
    33  type jwtAuth struct {
    34  }
    35  
    36  func (j *jwtAuth) ValidateToken(token string) (bool, error) {
    37  	return false, nil
    38  }
    39  
    40  func Test_Server_ServesHTML(t *testing.T) {
    41  	// given
    42  	tmpDIr, err := os.MkdirTemp("", "nodeuiversiontest")
    43  	assert.NoError(t, err)
    44  	defer os.Remove(tmpDIr)
    45  
    46  	config, err := versionmanager.NewVersionConfig("/tmp")
    47  	assert.NoError(t, err)
    48  
    49  	s := NewServer(
    50  		"localhost",
    51  		55565,
    52  		"localhost",
    53  		55564,
    54  		&jwtAuth{},
    55  		requests.NewHTTPClient("0.0.0.0", requests.DefaultTimeout),
    56  		config,
    57  	)
    58  	s.discovery = &mockDiscovery{}
    59  	s.Serve()
    60  	time.Sleep(time.Millisecond * 100)
    61  
    62  	// when
    63  	nilResp, err := http.Get("http://:55565/")
    64  	assert.Nil(t, err)
    65  	defer nilResp.Body.Close()
    66  
    67  	// then
    68  	root, err := html.Parse(nilResp.Body)
    69  	assert.Nil(t, err)
    70  	body := hasBody(root)
    71  	assert.True(t, body)
    72  
    73  	s.Stop()
    74  }
    75  
    76  func hasBody(n *html.Node) bool {
    77  	fs := n.FirstChild
    78  	if fs == nil {
    79  		return false
    80  	}
    81  	if fs.Type == html.ElementNode && n.Data == "body" {
    82  		return true
    83  	}
    84  
    85  	return hasBody(fs.NextSibling)
    86  }
    87  
    88  type mockDiscovery struct{}
    89  
    90  func (md *mockDiscovery) Start() error { return nil }
    91  func (md *mockDiscovery) Stop() error  { return nil }