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

     1  /*
     2   * Copyright (C) 2017 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 tequilapi
    19  
    20  import (
    21  	"net"
    22  	"os"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/mysteriumnetwork/node/core/node"
    27  
    28  	"github.com/mysteriumnetwork/node/tequilapi/endpoints"
    29  
    30  	"github.com/gin-gonic/gin"
    31  
    32  	"github.com/stretchr/testify/assert"
    33  	"github.com/stretchr/testify/suite"
    34  )
    35  
    36  type tequilapiTestSuite struct {
    37  	suite.Suite
    38  	server APIServer
    39  	client *testClient
    40  }
    41  
    42  func (testSuite *tequilapiTestSuite) SetupSuite() {
    43  	listener, err := net.Listen("tcp", "localhost:0")
    44  	assert.Nil(testSuite.T(), err)
    45  	testSuite.server, err = NewServer(listener, *node.GetOptions(), nil, []func(e *gin.Engine) error{func(e *gin.Engine) error {
    46  		e.GET("/healthcheck", endpoints.HealthCheckEndpointFactory(time.Now, os.Getpid).HealthCheck)
    47  		return nil
    48  	}})
    49  	assert.NoError(testSuite.T(), err)
    50  
    51  	testSuite.server.StartServing()
    52  	address, err := testSuite.server.Address()
    53  	assert.NoError(testSuite.T(), err)
    54  	testSuite.client = NewTestClient(testSuite.T(), address)
    55  }
    56  
    57  func (testSuite *tequilapiTestSuite) TestHealthCheckReturnsExpectedResponse() {
    58  	resp := testSuite.client.Get("/healthcheck")
    59  
    60  	expectJSONStatus200(testSuite.T(), resp, 200)
    61  
    62  	var jsonMap map[string]interface{}
    63  	parseResponseAsJSON(testSuite.T(), resp, &jsonMap)
    64  	assert.NotEmpty(testSuite.T(), jsonMap["uptime"])
    65  }
    66  
    67  func (testSuite *tequilapiTestSuite) TearDownSuite() {
    68  	testSuite.server.Stop()
    69  	testSuite.server.Wait()
    70  }
    71  
    72  func TestTequilapiTestSuite(t *testing.T) {
    73  	suite.Run(t, new(tequilapiTestSuite))
    74  }