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
|
|
|
|
2019-06-11 16:23:20 +00:00
|
|
|
. "github.com/tus/tusd/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{
|
2018-11-10 20:10:38 +00:00
|
|
|
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Upload-Length, Upload-Offset, Tus-Resumable, Upload-Metadata, Upload-Defer-Length, Upload-Concat",
|
2016-10-13 16:08:34 +00:00
|
|
|
"Access-Control-Allow-Methods": "POST, GET, HEAD, PATCH, DELETE, OPTIONS",
|
|
|
|
"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()
|
|
|
|
res.HeaderMap.Set("Access-Control-Allow-Headers", "HEADER")
|
|
|
|
res.HeaderMap.Set("Access-Control-Allow-Methods", "METHOD")
|
|
|
|
handler.ServeHTTP(res, req)
|
|
|
|
|
|
|
|
headers := res.HeaderMap["Access-Control-Allow-Headers"]
|
|
|
|
methods := res.HeaderMap["Access-Control-Allow-Methods"]
|
|
|
|
|
|
|
|
if headers[0] != "HEADER" {
|
|
|
|
t.Errorf("expected header to contain HEADER but got: %#v", headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
if methods[0] != "METHOD" {
|
|
|
|
t.Errorf("expected header to contain HEADER but got: %#v", methods)
|
|
|
|
}
|
|
|
|
})
|
2015-02-09 18:37:06 +00:00
|
|
|
}
|