2019-06-11 16:23:20 +00:00
|
|
|
package handler_test
|
2015-02-09 18:37:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-04-12 09:52:02 +00:00
|
|
|
"net/http/httptest"
|
2015-02-09 18:37:06 +00:00
|
|
|
"testing"
|
2015-12-25 21:33:27 +00:00
|
|
|
|
2023-03-08 12:03:18 +00:00
|
|
|
. "github.com/tus/tusd/v2/pkg/handler"
|
2015-02-09 18:37:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCORS(t *testing.T) {
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "Preflight", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2016-10-13 16:08:34 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 16:08:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "OPTIONS",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Origin": "tus.io",
|
|
|
|
},
|
|
|
|
Code: http.StatusOK,
|
|
|
|
ResHeader: map[string]string{
|
2020-04-06 10:24:05 +00:00
|
|
|
"Access-Control-Allow-Headers": "Authorization, Origin, X-Requested-With, X-Request-ID, X-HTTP-Method-Override, Content-Type, Upload-Length, Upload-Offset, Tus-Resumable, Upload-Metadata, Upload-Defer-Length, Upload-Concat",
|
2022-03-28 21:43:35 +00:00
|
|
|
"Access-Control-Allow-Methods": "POST, HEAD, PATCH, OPTIONS, GET, DELETE",
|
|
|
|
"Access-Control-Max-Age": "86400",
|
|
|
|
"Access-Control-Allow-Origin": "tus.io",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
|
|
|
SubTest(t, "Conditional allow methods", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
StoreComposer: composer,
|
|
|
|
DisableTermination: true,
|
|
|
|
DisableDownload: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "OPTIONS",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Origin": "tus.io",
|
|
|
|
},
|
|
|
|
Code: http.StatusOK,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Access-Control-Allow-Headers": "Authorization, Origin, X-Requested-With, X-Request-ID, X-HTTP-Method-Override, Content-Type, Upload-Length, Upload-Offset, Tus-Resumable, Upload-Metadata, Upload-Defer-Length, Upload-Concat",
|
|
|
|
"Access-Control-Allow-Methods": "POST, HEAD, PATCH, OPTIONS",
|
2016-10-13 16:08:34 +00:00
|
|
|
"Access-Control-Max-Age": "86400",
|
|
|
|
"Access-Control-Allow-Origin": "tus.io",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
2016-03-11 19:46:34 +00:00
|
|
|
})
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "Request", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2016-10-13 16:08:34 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 16:08:34 +00:00
|
|
|
})
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2016-10-13 16:08:34 +00:00
|
|
|
(&httpTest{
|
|
|
|
Name: "Actual request",
|
|
|
|
Method: "GET",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Origin": "tus.io",
|
|
|
|
},
|
|
|
|
Code: http.StatusMethodNotAllowed,
|
|
|
|
ResHeader: map[string]string{
|
2018-11-10 20:10:38 +00:00
|
|
|
"Access-Control-Expose-Headers": "Upload-Offset, Location, Upload-Length, Tus-Version, Tus-Resumable, Tus-Max-Size, Tus-Extension, Upload-Metadata, Upload-Defer-Length, Upload-Concat",
|
2016-10-13 16:08:34 +00:00
|
|
|
"Access-Control-Allow-Origin": "tus.io",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2017-04-12 09:52:02 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "AppendHeaders", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2017-04-12 09:52:02 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2017-04-12 09:52:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("OPTIONS", "", nil)
|
|
|
|
req.Header.Set("Tus-Resumable", "1.0.0")
|
|
|
|
req.Header.Set("Origin", "tus.io")
|
|
|
|
req.Host = "tus.io"
|
|
|
|
|
|
|
|
res := httptest.NewRecorder()
|
2019-10-08 08:30:19 +00:00
|
|
|
res.Header().Set("Access-Control-Allow-Headers", "HEADER")
|
|
|
|
res.Header().Set("Access-Control-Allow-Methods", "METHOD")
|
2017-04-12 09:52:02 +00:00
|
|
|
handler.ServeHTTP(res, req)
|
|
|
|
|
2019-10-08 08:30:19 +00:00
|
|
|
headers := res.Header()["Access-Control-Allow-Headers"]
|
|
|
|
methods := res.Header()["Access-Control-Allow-Methods"]
|
2017-04-12 09:52:02 +00:00
|
|
|
|
|
|
|
if headers[0] != "HEADER" {
|
|
|
|
t.Errorf("expected header to contain HEADER but got: %#v", headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
if methods[0] != "METHOD" {
|
2019-10-08 08:30:19 +00:00
|
|
|
t.Errorf("expected header to contain METHOD but got: %#v", methods)
|
2017-04-12 09:52:02 +00:00
|
|
|
}
|
|
|
|
})
|
2023-03-26 22:11:41 +00:00
|
|
|
|
|
|
|
SubTest(t, "Disable CORS", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
StoreComposer: composer,
|
|
|
|
DisableCors: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "OPTIONS",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Origin": "tus.io",
|
|
|
|
},
|
|
|
|
Code: http.StatusOK,
|
|
|
|
ResHeader: map[string]string{},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2015-02-09 18:37:06 +00:00
|
|
|
}
|