mirror of
https://github.com/hanzoai/team.git
synced 2026-09-30 01:57:35 +00:00
The base moves off /v1/team for two reasons that point the same way. It collided: /v1/team on hanzo.team is a carve-out routed to the older cloud service, so the moment this image shipped its own API would have been answered by something else -- a 404 on our own endpoints, on the host we serve from. And workspace is the better noun. It is what the product calls itself in its own words, and it does not read as though the API belongs to one team among several. No /api/ anywhere in a path we own. Co-authored-by: Hanzo Dev <[email protected]>
97 lines
3.1 KiB
Go
97 lines
3.1 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package web
|
|
|
|
import (
|
|
"bufio"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type TestHandler struct {
|
|
TestFunc func(w http.ResponseWriter, r *http.Request)
|
|
}
|
|
|
|
func (h *TestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
h.TestFunc(w, r)
|
|
}
|
|
|
|
type responseRecorderHijack struct {
|
|
httptest.ResponseRecorder
|
|
}
|
|
|
|
func (r *responseRecorderHijack) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
r.WriteHeader(http.StatusOK)
|
|
return nil, nil, nil
|
|
}
|
|
|
|
func newResponseWithHijack(original *httptest.ResponseRecorder) *responseRecorderHijack {
|
|
return &responseRecorderHijack{*original}
|
|
}
|
|
|
|
func TestStatusCodeIsAccessible(t *testing.T) {
|
|
resp := newWrappedWriter(httptest.NewRecorder())
|
|
req := httptest.NewRequest("GET", "/v1/workspace/test", nil)
|
|
handler := TestHandler{func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
}}
|
|
handler.ServeHTTP(resp, req)
|
|
assert.Equal(t, http.StatusBadRequest, resp.StatusCode())
|
|
}
|
|
|
|
func TestStatusCodeShouldBe200IfNotHeaderWritten(t *testing.T) {
|
|
resp := newWrappedWriter(httptest.NewRecorder())
|
|
req := httptest.NewRequest("GET", "/v1/workspace/test", nil)
|
|
handler := TestHandler{func(w http.ResponseWriter, r *http.Request) {
|
|
n, err := w.Write([]byte{})
|
|
require.NoError(t, err, "Failed to write response")
|
|
require.Equal(t, 0, n, "Expected to write 0 bytes")
|
|
}}
|
|
handler.ServeHTTP(resp, req)
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode())
|
|
}
|
|
|
|
func TestForUnsupportedHijack(t *testing.T) {
|
|
resp := newWrappedWriter(httptest.NewRecorder())
|
|
req := httptest.NewRequest("GET", "/v1/workspace/test", nil)
|
|
handler := TestHandler{func(w http.ResponseWriter, r *http.Request) {
|
|
conn, rw, err := w.(*responseWriterWrapper).Hijack()
|
|
assert.Error(t, err)
|
|
assert.Equal(t, "Hijacker interface not supported by the wrapped ResponseWriter", err.Error())
|
|
assert.Nil(t, conn, "Expected nil connection")
|
|
assert.Nil(t, rw, "Expected nil buffer")
|
|
}}
|
|
handler.ServeHTTP(resp, req)
|
|
}
|
|
|
|
func TestForSupportedHijack(t *testing.T) {
|
|
resp := newWrappedWriter(newResponseWithHijack(httptest.NewRecorder()))
|
|
req := httptest.NewRequest("GET", "/v1/workspace/test", nil)
|
|
handler := TestHandler{func(w http.ResponseWriter, r *http.Request) {
|
|
conn, rw, err := w.(*responseWriterWrapper).Hijack()
|
|
require.NoError(t, err, "Hijack should succeed with supporting ResponseWriter")
|
|
assert.Nil(t, conn, "Expected nil connection from test implementation")
|
|
assert.Nil(t, rw, "Expected nil buffer from test implementation")
|
|
}}
|
|
handler.ServeHTTP(resp, req)
|
|
}
|
|
|
|
func TestForSupportedFlush(t *testing.T) {
|
|
resp := newWrappedWriter(httptest.NewRecorder())
|
|
req := httptest.NewRequest("GET", "/v1/workspace/test", nil)
|
|
handler := TestHandler{func(w http.ResponseWriter, r *http.Request) {
|
|
n, err := w.Write([]byte{})
|
|
require.NoError(t, err, "Failed to write response")
|
|
require.Equal(t, 0, n, "Expected to write 0 bytes")
|
|
w.(*responseWriterWrapper).Flush()
|
|
}}
|
|
handler.ServeHTTP(resp, req)
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode())
|
|
}
|