photoprism/pkg/http/header/location_test.go
Michael Mayer ce304abd2c API: Update endpoints to return HTTP 201 when a new resource was created
Signed-off-by: Michael Mayer <michael@photoprism.app>
2025-10-20 16:46:59 +02:00

43 lines
1 KiB
Go

package header
import (
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestSetLocationWithBase(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest("POST", "/api/v1/albums", nil)
SetLocation(c, "/api/v1/albums", "abc123")
assert.Equal(t, "/api/v1/albums/abc123", c.Writer.Header().Get(Location))
}
func TestSetLocationUsesRequestPath(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest("POST", "/api/v1/services", nil)
SetLocation(c, "", "99")
assert.Equal(t, "/api/v1/services/99", c.Writer.Header().Get(Location))
}
func TestSetLocationTrimsSegments(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest("POST", "/api/v1/markers/", nil)
SetLocation(c, "/api/v1/markers/", "/m1/")
assert.Equal(t, "/api/v1/markers/m1", c.Writer.Header().Get(Location))
}
func TestSetLocationEmpty(t *testing.T) {
SetLocation(nil)
}