Skip to content

Instantly share code, notes, and snippets.

@umit
Created August 6, 2021 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umit/0d8bce7efc4a3bfed400707509abef4c to your computer and use it in GitHub Desktop.
Save umit/0d8bce7efc4a3bfed400707509abef4c to your computer and use it in GitHub Desktop.
Testcontainers with Tyk.
import (
"encoding/json"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
func Test_GetHelloEndpoint(t *testing.T) {
// git clone https://github.com/TykTechnologies/tyk-gateway-docker
composeFilePaths := []string{"./tyk-gateway-docker/docker-compose.yml"}
identifier := "tyk-container"
compose := testcontainers.NewLocalDockerCompose(composeFilePaths, identifier)
tyk := compose.
WithCommand([]string{"up", "-d"}).
WithExposedService("tyk-gateway_1", 8080,
wait.ForLog("\"API Loaded\"").WithStartupTimeout(10*time.Second)).
WithExposedService("tyk-redis_1", 6379,
wait.ForLog("Server initialized\n\n").WithStartupTimeout(10*time.Second)).
WithEnv(map[string]string{
"TYK_GW_SECRET": "foo",
})
tyk.Invoke()
defer tyk.Down()
resp, err := http.Get("http://localhost:8080/hello")
if err != nil {
assert.Error(t, err, "Error occurred while reading from http://localhost:8080/hello")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
assert.Errorf(t, err, "Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
}
var response Response
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
assert.Errorf(t, err, "Response decode is failed.")
}
assert.Equal(t, "v3.2.1", response.Version)
assert.Equal(t, "Tyk GW", response.Description)
assert.Equal(t, "pass", response.Status)
assert.Equal(t, "datastore", response.Details.Redis.ComponentType)
assert.Equal(t, "pass", response.Details.Redis.Status)
}
type Response struct {
Status string `json:"status"`
Version string `json:"version"`
Description string `json:"description"`
Details struct {
Redis struct {
Status string `json:"status"`
ComponentType string `json:"componentType"`
Time time.Time `json:"time"`
} `json:"redis"`
} `json:"details"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment