mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
* feat: Add app support This adds apps as a property to a workspace agent. The resource is added to the Terraform provider here: https://github.com/coder/terraform-provider-coder/pull/17 Apps will be opened in the dashboard or via the CLI with `coder open <name>`. If `command` is specified, a terminal will appear locally and in the web. If `target` is specified, the browser will open to an exposed instance of that target. * Compare fields in apps test * Update Terraform provider to use relative path * Add some basic structure for routing * chore: Remove interface from coderd and lift API surface Abstracting coderd into an interface added misdirection because the interface was never intended to be fulfilled outside of a single implementation. This lifts the abstraction, and attaches all handlers to a root struct named `*coderd.API`. * Add basic proxy logic * Add proxying based on path * Add app proxying for wildcards * Add wsconncache * fix: Race when writing to a closed pipe This is such an intermittent race it's difficult to track, but regardless this is an improvement to the code. * fix: Race when writing to a closed pipe This is such an intermittent race it's difficult to track, but regardless this is an improvement to the code. * fix: Race when writing to a closed pipe This is such an intermittent race it's difficult to track, but regardless this is an improvement to the code. * fix: Race when writing to a closed pipe This is such an intermittent race it's difficult to track, but regardless this is an improvement to the code. * Add workspace route proxying endpoint - Makes the workspace conn cache concurrency-safe - Reduces unnecessary open checks in `peer.Channel` - Fixes the use of a temporary context when dialing a workspace agent * Add embed errors * chore: Refactor site to improve testing It was difficult to develop this package due to the embed build tag being mandatory on the tests. The logic to test doesn't require any embedded files. * Add test for error handler * Remove unused access url * Add RBAC tests * Fix dial agent syntax * Fix linting errors * Fix gen * Fix icon required * Adjust migration number * Fix proxy error status code * Fix empty db lookup
210 lines
5.3 KiB
Go
210 lines
5.3 KiB
Go
package site_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"testing/fstest"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/site"
|
|
)
|
|
|
|
func TestCaching(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Create a test server
|
|
rootFS := fstest.MapFS{
|
|
"bundle.js": &fstest.MapFile{},
|
|
"image.png": &fstest.MapFile{},
|
|
"static/image.png": &fstest.MapFile{},
|
|
"favicon.ico": &fstest.MapFile{
|
|
Data: []byte("folderFile"),
|
|
},
|
|
|
|
"service-worker.js": &fstest.MapFile{},
|
|
"index.html": &fstest.MapFile{
|
|
Data: []byte("folderFile"),
|
|
},
|
|
"terminal.html": &fstest.MapFile{
|
|
Data: []byte("folderFile"),
|
|
},
|
|
}
|
|
|
|
srv := httptest.NewServer(site.Handler(rootFS))
|
|
defer srv.Close()
|
|
|
|
// Create a context
|
|
ctx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Second)
|
|
defer cancelFunc()
|
|
|
|
testCases := []struct {
|
|
path string
|
|
isExpectingCache bool
|
|
}{
|
|
{"/bundle.js", true},
|
|
{"/image.png", true},
|
|
{"/static/image.png", true},
|
|
{"/favicon.ico", true},
|
|
|
|
{"/", false},
|
|
{"/service-worker.js", false},
|
|
{"/index.html", false},
|
|
{"/double/nested/terminal.html", false},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+testCase.path, nil)
|
|
require.NoError(t, err, "create request")
|
|
|
|
res, err := srv.Client().Do(req)
|
|
require.NoError(t, err, "get index")
|
|
|
|
cache := res.Header.Get("Cache-Control")
|
|
if testCase.isExpectingCache {
|
|
require.Equalf(t, "public, max-age=31536000, immutable", cache, "expected %w file to have immutable cache", testCase.path)
|
|
} else {
|
|
require.Equalf(t, "", cache, "expected %w file to not have immutable cache header", testCase.path)
|
|
}
|
|
|
|
require.NoError(t, res.Body.Close(), "closing response")
|
|
}
|
|
}
|
|
|
|
func TestServingFiles(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Create a test server
|
|
rootFS := fstest.MapFS{
|
|
"index.html": &fstest.MapFile{
|
|
Data: []byte("index-bytes"),
|
|
},
|
|
"favicon.ico": &fstest.MapFile{
|
|
Data: []byte("favicon-bytes"),
|
|
},
|
|
"dashboard.js": &fstest.MapFile{
|
|
Data: []byte("dashboard-js-bytes"),
|
|
},
|
|
"dashboard.css": &fstest.MapFile{
|
|
Data: []byte("dashboard-css-bytes"),
|
|
},
|
|
}
|
|
|
|
srv := httptest.NewServer(site.Handler(rootFS))
|
|
defer srv.Close()
|
|
|
|
// Create a context
|
|
ctx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Second)
|
|
defer cancelFunc()
|
|
|
|
var testCases = []struct {
|
|
path string
|
|
expected string
|
|
}{
|
|
// Index cases
|
|
{"/", "index-bytes"},
|
|
{"/index.html", "index-bytes"},
|
|
{"/nested", "index-bytes"},
|
|
{"/nested/", "index-bytes"},
|
|
{"/nested/index.html", "index-bytes"},
|
|
|
|
// These are nested paths that should lead back to index. We don't
|
|
// allow nested JS or CSS files.
|
|
{"/double/nested", "index-bytes"},
|
|
{"/double/nested/", "index-bytes"},
|
|
{"/double/nested/index.html", "index-bytes"},
|
|
{"/nested/dashboard.js", "index-bytes"},
|
|
{"/nested/dashboard.css", "index-bytes"},
|
|
{"/double/nested/dashboard.js", "index-bytes"},
|
|
{"/double/nested/dashboard.css", "index-bytes"},
|
|
|
|
// Favicon cases
|
|
// The favicon is always root-referenced in index.html:
|
|
{"/favicon.ico", "favicon-bytes"},
|
|
|
|
// JS, CSS cases
|
|
{"/dashboard.js", "dashboard-js-bytes"},
|
|
{"/dashboard.css", "dashboard-css-bytes"},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
path := srv.URL + testCase.path
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", path, nil)
|
|
require.NoError(t, err)
|
|
resp, err := http.DefaultClient.Do(req)
|
|
require.NoError(t, err, "get file")
|
|
data, _ := io.ReadAll(resp.Body)
|
|
require.Equal(t, string(data), testCase.expected, "Verify file: "+testCase.path)
|
|
err = resp.Body.Close()
|
|
require.NoError(t, err)
|
|
}
|
|
}
|
|
|
|
func TestShouldCacheFile(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var testCases = []struct {
|
|
reqFile string
|
|
expected bool
|
|
}{
|
|
{"123456789.js", true},
|
|
{"apps/app/code/terminal.css", true},
|
|
{"image.png", true},
|
|
{"static/image.png", true},
|
|
{"static/images/section-a/image.jpeg", true},
|
|
|
|
{"service-worker.js", false},
|
|
{"dashboard.html", false},
|
|
{"apps/app/code/terminal.html", false},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
got := site.ShouldCacheFile(testCase.reqFile)
|
|
require.Equal(t, testCase.expected, got, fmt.Sprintf("Expected ShouldCacheFile(%s) to be %t", testCase.reqFile, testCase.expected))
|
|
}
|
|
}
|
|
|
|
func TestServeAPIResponse(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Create a test server
|
|
rootFS := fstest.MapFS{
|
|
"index.html": &fstest.MapFile{
|
|
Data: []byte(`{"code":{{ .APIResponse.StatusCode }},"message":"{{ .APIResponse.Message }}"}`),
|
|
},
|
|
}
|
|
|
|
apiResponse := site.APIResponse{
|
|
StatusCode: http.StatusBadGateway,
|
|
Message: "This could be an error message!",
|
|
}
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
r = r.WithContext(site.WithAPIResponse(r.Context(), apiResponse))
|
|
site.Handler(rootFS).ServeHTTP(w, r)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
req, err := http.NewRequestWithContext(context.Background(), "GET", srv.URL, nil)
|
|
require.NoError(t, err)
|
|
resp, err := http.DefaultClient.Do(req)
|
|
require.NoError(t, err)
|
|
var body struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
}
|
|
data, err := io.ReadAll(resp.Body)
|
|
require.NoError(t, err)
|
|
t.Logf("resp: %q", data)
|
|
err = json.Unmarshal(data, &body)
|
|
require.NoError(t, err)
|
|
require.Equal(t, apiResponse.StatusCode, body.Code)
|
|
require.Equal(t, apiResponse.Message, body.Message)
|
|
}
|