Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: platformsh/legacy-cli
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 96d61da8d0fdba331cf0261d1d785fed9230783b
Choose a base ref
..
head repository: platformsh/legacy-cli
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a1704bfcd94e43b28e926e52bac96e2f84a3e024
Choose a head ref
Showing with 121 additions and 0 deletions.
  1. +121 −0 go-tests/route_list_test.go
121 changes: 121 additions & 0 deletions go-tests/route_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package tests

import (
"encoding/base64"
"encoding/json"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/platformsh/cli/pkg/mockapi"
)

func TestRouteList(t *testing.T) {
authServer := mockapi.NewAuthServer(t)
defer authServer.Close()

apiHandler := mockapi.NewHandler(t)

projectID := "iewohthoo1ein"

apiHandler.SetProjects([]*mockapi.Project{{
ID: projectID,
Links: mockapi.MakeHALLinks("self=/projects/"+projectID,
"environments=/projects/"+projectID+"/environments"),
DefaultBranch: "main",
}})

main := makeEnv(projectID, "main", "production", "active", nil)
main.SetCurrentDeployment(&mockapi.Deployment{
WebApps: map[string]mockapi.App{
"app": {Name: "app", Type: "golang:1.23", Size: "M", Disk: 2048, Mounts: map[string]mockapi.Mount{}},
},
Routes: mockRoutes(),
Links: mockapi.MakeHALLinks("self=/projects/" + projectID + "/environments/main/deployment/current"),
})
envs := []*mockapi.Environment{main}

apiHandler.SetEnvironments(envs)

apiServer := httptest.NewServer(apiHandler)
defer apiServer.Close()

f := newCommandFactory(t, apiServer.URL, authServer.URL)

assertTrimmed(t, `
+-------------------+----------+---------------------------+
| Route | Type | To |
+-------------------+----------+---------------------------+
| http://{default} | redirect | https://main.example.com/ |
| https://{default} | upstream | app:http |
+-------------------+----------+---------------------------+
`, f.Run("routes", "-p", projectID, "-e", ".", "--refresh"))

assert.Equal(t, "upstream\n", f.Run("route:get", "-p", projectID, "-e", ".", "https://{default}", "-P", "type"))
}

func TestRouteListLocal(t *testing.T) {
f := &cmdFactory{t: t}
routes, err := json.Marshal(mockRoutes())
require.NoError(t, err)
f.extraEnv = []string{"PLATFORM_ROUTES=" + base64.StdEncoding.EncodeToString(routes)}

assertTrimmed(t, `
+-------------------+----------+---------------------------+
| Route | Type | To |
+-------------------+----------+---------------------------+
| http://{default} | redirect | https://main.example.com/ |
| https://{default} | upstream | app:http |
+-------------------+----------+---------------------------+
`, f.Run("route:list"))

assert.Equal(t, "redirect\n", f.Run("route:get", "http://{default}", "--property", "type"))
assert.Equal(t, "https://main.example.com/\n", f.Run("route:get", "http://{default}", "--property", "to"))
}

// TODO make a Route type
func mockRoutes() map[string]any {
return map[string]any{
"https://main.example.com/": map[string]any{
"primary": true,
"id": "app",
"production_url": "https://main.example.com/",
"attributes": map[string]any{},
"type": "upstream",
"original_url": "https://{default}",
"http_access": map[string]any{
"is_enabled": true,
"addresses": []string{},
"basic_auth": map[string]any{},
},
"restrict_robots": true,
"cache": map[string]any{
"enabled": true,
"default_ttl": 0,
"cookies": []string{"*"},
"headers": []string{"Accept", "Accept-Language"},
},
"ssi": map[string]any{
"enabled": false,
},
"upstream": "app:http",
},
"http://main.example.com/": map[string]any{
"primary": true,
"id": "app",
"production_url": "http://main.example.com/",
"attributes": map[string]any{},
"type": "redirect",
"original_url": "http://{default}",
"http_access": map[string]any{
"is_enabled": true,
"addresses": []string{},
"basic_auth": map[string]any{},
},
"restrict_robots": true,
"to": "https://main.example.com/",
},
}
}