From d6bc50ff12f76128c623aaff1197423206b478bf Mon Sep 17 00:00:00 2001 From: Marius Date: Tue, 10 May 2016 11:58:43 +0200 Subject: [PATCH] Use 200 OK in responses for preflight requests Some browsers only accept 200 OK and not 204 No Content as respones codes for preflight requests. See tus/tus-js-client#34 --- concat_test.go | 2 +- cors_test.go | 2 +- options_test.go | 2 +- terminate_test.go | 2 +- unrouted_handler.go | 9 ++++++++- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/concat_test.go b/concat_test.go index 11d6e20..4f522fb 100644 --- a/concat_test.go +++ b/concat_test.go @@ -48,7 +48,7 @@ func TestConcatPartial(t *testing.T) { ResHeader: map[string]string{ "Tus-Extension": "creation,concatenation", }, - Code: http.StatusNoContent, + Code: http.StatusOK, }).Run(handler, t) (&httpTest{ diff --git a/cors_test.go b/cors_test.go index 5033b80..875311a 100644 --- a/cors_test.go +++ b/cors_test.go @@ -20,7 +20,7 @@ func TestCORS(t *testing.T) { ReqHeader: map[string]string{ "Origin": "tus.io", }, - Code: http.StatusNoContent, + Code: http.StatusOK, ResHeader: map[string]string{ "Access-Control-Allow-Headers": "", "Access-Control-Allow-Methods": "", diff --git a/options_test.go b/options_test.go index 1cc304b..206fdf2 100644 --- a/options_test.go +++ b/options_test.go @@ -18,7 +18,7 @@ func TestOptions(t *testing.T) { (&httpTest{ Name: "Successful request", Method: "OPTIONS", - Code: http.StatusNoContent, + Code: http.StatusOK, ResHeader: map[string]string{ "Tus-Extension": "creation", "Tus-Version": "1.0.0", diff --git a/terminate_test.go b/terminate_test.go index 3941dba..ba15164 100644 --- a/terminate_test.go +++ b/terminate_test.go @@ -46,7 +46,7 @@ func TestTerminate(t *testing.T) { ResHeader: map[string]string{ "Tus-Extension": "creation,termination", }, - Code: http.StatusNoContent, + Code: http.StatusOK, }).Run(handler, t) (&httpTest{ diff --git a/unrouted_handler.go b/unrouted_handler.go index 2e3c5a5..2484fdf 100644 --- a/unrouted_handler.go +++ b/unrouted_handler.go @@ -158,7 +158,14 @@ func (handler *UnroutedHandler) Middleware(h http.Handler) http.Handler { header.Set("Tus-Version", "1.0.0") header.Set("Tus-Extension", handler.extensions) - w.WriteHeader(http.StatusNoContent) + // Although the 204 No Content status code is a better fit in this case, + // since we do not have a response body included, we cannot use it here + // as some browsers only accept 200 OK as successful response to a + // preflight request. If we send them the 204 No Content the response + // will be ignored or interpreted as a rejection. + // For example, the Presto engine, which is used in older versions of + // Opera, Opera Mobile and Opera Mini, handles CORS this way. + w.WriteHeader(http.StatusOK) return }