github.com/someshkoli/terratest@v0.41.1/modules/http-helper/dummy_server_test.go (about)

     1  package http_helper
     2  
     3  import (
     4  	"crypto/tls"
     5  	"fmt"
     6  	"io"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/gruntwork-io/terratest/modules/random"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestRunDummyServer(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	uniqueID := random.UniqueId()
    18  	text := fmt.Sprintf("dummy-server-%s", uniqueID)
    19  
    20  	listener, port := RunDummyServer(t, text)
    21  	defer shutDownServer(t, listener)
    22  
    23  	url := fmt.Sprintf("http://localhost:%d", port)
    24  	HttpGetWithValidation(t, url, &tls.Config{}, 200, text)
    25  }
    26  
    27  func TestContinuouslyCheck(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	uniqueID := random.UniqueId()
    31  	text := fmt.Sprintf("dummy-server-%s", uniqueID)
    32  	stopChecking := make(chan bool, 1)
    33  
    34  	listener, port := RunDummyServer(t, text)
    35  
    36  	url := fmt.Sprintf("http://localhost:%d", port)
    37  	wg, responses := ContinuouslyCheckUrl(t, url, stopChecking, 1*time.Second)
    38  	defer func() {
    39  		stopChecking <- true
    40  		counts := 0
    41  		for response := range responses {
    42  			counts++
    43  			assert.Equal(t, response.StatusCode, 200)
    44  			assert.Equal(t, response.Body, text)
    45  		}
    46  		wg.Wait()
    47  		// Make sure we made at least one call
    48  		assert.NotEqual(t, counts, 0)
    49  		shutDownServer(t, listener)
    50  	}()
    51  	time.Sleep(5 * time.Second)
    52  }
    53  
    54  func shutDownServer(t *testing.T, listener io.Closer) {
    55  	err := listener.Close()
    56  	assert.NoError(t, err)
    57  }