Skip to content

Latest commit

 

History

History
57 lines (46 loc) · 1.14 KB

GO.md

File metadata and controls

57 lines (46 loc) · 1.14 KB

Tutorial

Testing Projects library

###Simple http test

package main

import (
	"io/ioutil"
	"net/http"
	"os"
	"strings"
	"testing"

	rest "github.com/hashicorp/go-retryablehttp"
)

func TestService(t *testing.T) {
	port := os.Getenv("PORT")
	if port == "" {
		port = "8080"
	}

	url := os.Getenv("SERVICE_URL")
	if url == "" {
		url = "http://localhost:" + port
	}

	resp, err := rest.Get(url + "/")
	resp.Header.Add("", "")
	if err != nil {
		t.Fatalf("retry.Get: %v", err)
	}

	if got := resp.StatusCode; got != http.StatusOK {
		t.Errorf("HTTP Response: got %q, want %q", got, http.StatusOK)
	}

	out, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatalf("ioutil.ReadAll: %v", err)
	}

	want := "Congratulations, you successfully deployed a container image to Cloud Run"
	if !strings.Contains(string(out), want) {
		t.Errorf("HTTP Response: body does not include %q", want)
	}
}