Compare commits

...

5 Commits

Author SHA1 Message Date
Pijus Kamandulis
a6b5d32ff7
Merge pull request #4 from pikami/erikzeneco/serve_request_paths_with_trailing_slashes
Erikzeneco/serve request paths with trailing slashes
2024-10-29 18:06:56 +02:00
Pijus Kamandulis
0e98e3481a Strip trailing slash using middleware 2024-10-28 20:20:52 +02:00
Erik Zentveld
827046f634 re-add removed blank lines 2024-10-28 16:18:49 +01:00
Erik Zentveld
475d586dc5 Merge branch 'master' into serve_request_paths_with_trailing_slashes 2024-10-28 14:37:01 +01:00
Erik Zentveld
9abef691d6 serve request paths with trailing slashes, as sent by python client 2024-10-28 13:29:26 +01:00
3 changed files with 63 additions and 1 deletions

View File

@ -0,0 +1,18 @@
package middleware
import (
"github.com/gin-gonic/gin"
)
func StripTrailingSlashes(r *gin.Engine) gin.HandlerFunc {
return func(c *gin.Context) {
path := c.Request.URL.Path
if len(path) > 1 && path[len(path)-1] == '/' {
c.Request.URL.Path = path[:len(path)-1]
r.HandleContext(c)
c.Abort()
return
}
c.Next()
}
}

View File

@ -13,12 +13,15 @@ import (
)
func CreateRouter() *gin.Engine {
router := gin.Default()
router := gin.Default(func(e *gin.Engine) {
e.RedirectTrailingSlash = false
})
if config.Config.Debug {
router.Use(middleware.RequestLogger())
}
router.Use(middleware.StripTrailingSlashes(router))
router.Use(middleware.Authentication())
router.GET("/dbs/:databaseId/colls/:collId/pkranges", handlers.GetPartitionKeyRanges)

View File

@ -0,0 +1,41 @@
package tests_test
import (
"fmt"
"net/http"
"net/url"
"testing"
"time"
"github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/internal/authentication"
"github.com/stretchr/testify/assert"
)
// Request document with trailing slash like python cosmosdb client does.
func Test_Documents_Read_Trailing_Slash(t *testing.T) {
ts, _ := documents_InitializeDb(t)
defer ts.Close()
t.Run("Read doc with client that appends slash to path", func(t *testing.T) {
resourceIdTemplate := "dbs/%s/colls/%s/docs/%s"
path := fmt.Sprintf(resourceIdTemplate, testDatabaseName, testCollectionName, "12345")
testUrl := ts.URL + "/" + path + "/"
date := time.Now().Format(time.RFC1123)
signature := authentication.GenerateSignature("GET", "docs", path, date, config.Config.AccountKey)
httpClient := &http.Client{}
req, _ := http.NewRequest("GET", testUrl, nil)
req.Header.Add("x-ms-date", date)
req.Header.Add("authorization", "sig="+url.QueryEscape(signature))
res, err := httpClient.Do(req)
assert.Nil(t, err)
if res != nil {
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode, "Expected HTTP status 200 OK")
} else {
t.FailNow()
}
})
}