mirror of
https://github.com/simon987/ws_bucket.git
synced 2025-04-20 02:36:46 +00:00
Allow passing the upload token as query string for reading
This commit is contained in:
parent
b05c9f2b1a
commit
c41ed01184
@ -142,6 +142,9 @@ func (api *WebApi) Upload(ctx *fasthttp.RequestCtx) {
|
|||||||
func (api *WebApi) ReadUploadSlot(ctx *fasthttp.RequestCtx) {
|
func (api *WebApi) ReadUploadSlot(ctx *fasthttp.RequestCtx) {
|
||||||
|
|
||||||
tokenStr := string(ctx.Request.Header.Peek("X-Upload-Token"))
|
tokenStr := string(ctx.Request.Header.Peek("X-Upload-Token"))
|
||||||
|
if tokenStr == "" {
|
||||||
|
tokenStr = string(ctx.Request.URI().QueryArgs().Peek("token"))
|
||||||
|
}
|
||||||
|
|
||||||
slot := UploadSlot{}
|
slot := UploadSlot{}
|
||||||
err := api.db.Where("token=?", tokenStr).First(&slot).Error
|
err := api.db.Where("token=?", tokenStr).First(&slot).Error
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
package test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"github.com/fasthttp/websocket"
|
||||||
"github.com/simon987/ws_bucket/api"
|
"github.com/simon987/ws_bucket/api"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAllocateUploadInvalidMaxSize(t *testing.T) {
|
func TestAllocateUploadInvalidMaxSize(t *testing.T) {
|
||||||
@ -65,6 +70,33 @@ func TestDuplicateUploadSlot(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTokenInQueryString(t *testing.T) {
|
||||||
|
|
||||||
|
if allocateUploadSlot(api.AllocateUploadSlotRequest{
|
||||||
|
FileName: "test.png",
|
||||||
|
Token: "testquery",
|
||||||
|
MaxSize: 100,
|
||||||
|
}).Ok != true {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
conn := ws("testquery")
|
||||||
|
conn.WriteMessage(websocket.BinaryMessage, []byte("test"))
|
||||||
|
conn.Close()
|
||||||
|
|
||||||
|
time.Sleep(time.Millisecond * 20)
|
||||||
|
r, err := http.Get("http://" + api.GetServerAddress() + "/slot?token=testquery")
|
||||||
|
handleErr(err)
|
||||||
|
|
||||||
|
data, err := ioutil.ReadAll(r.Body)
|
||||||
|
handleErr(err)
|
||||||
|
|
||||||
|
if bytes.Compare(data, []byte("test")) != 0 {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func allocateUploadSlot(request api.AllocateUploadSlotRequest) (ar *api.GenericResponse) {
|
func allocateUploadSlot(request api.AllocateUploadSlotRequest) (ar *api.GenericResponse) {
|
||||||
resp := Post("/slot", request)
|
resp := Post("/slot", request)
|
||||||
UnmarshalResponse(resp, &ar)
|
UnmarshalResponse(resp, &ar)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user