Setting ContentType in the HTTP responses (#1149)

* Also making auth test work in IE
This commit is contained in:
Pawel Kadluczka 2018-01-16 11:28:20 -08:00 committed by BrennanConroy
parent a862959566
commit 68aa609650
3 changed files with 46 additions and 34 deletions

View File

@ -484,8 +484,8 @@ describe('hubConnection', function () {
var message = '你好,世界!'; var message = '你好,世界!';
var hubConnection; var hubConnection;
getJwtToken('http://' + document.location.host + '/generateJwtToken') getJwtToken('http://' + document.location.host + '/generateJwtToken', done,
.then(jwtToken => { function(jwtToken) {
var options = { var options = {
transport: transportType, transport: transportType,
logging: signalR.LogLevel.Trace, logging: signalR.LogLevel.Trace,
@ -493,23 +493,24 @@ describe('hubConnection', function () {
return jwtToken; return jwtToken;
} }
}; };
hubConnection = new signalR.HubConnection('/authorizedhub', options); hubConnection = new signalR.HubConnection('/authorizedhub', options);
hubConnection.onclose(function (error) { hubConnection.onclose(function (error) {
expect(error).toBe(undefined); expect(error).toBe(undefined);
done(); done();
}); });
return hubConnection.start(); hubConnection.start()
}) .then(function() {
.then(function () { return hubConnection.invoke('Echo', message);
return hubConnection.invoke('Echo', message); })
}) .then(function(response) {
.then(function (response) { expect(response).toEqual(message);
expect(response).toEqual(message); return hubConnection.stop();
return hubConnection.stop(); })
}) .catch(function(e) {
.catch(function (e) { fail(e);
fail(e); done();
done(); });
}); });
}); });
@ -539,25 +540,25 @@ describe('hubConnection', function () {
}); });
}); });
function getJwtToken(url) { function getJwtToken(url, done, callback) {
return new Promise((resolve, reject) => { let xhr = new XMLHttpRequest();
let xhr = new XMLHttpRequest();
xhr.open('GET', url, true); xhr.open('GET', url, true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.send(); xhr.send();
xhr.onload = () => { xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) { if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response || xhr.responseText); callback(xhr.response || xhr.responseText);
}
else {
reject(new Error(xhr.statusText));
}
};
xhr.onerror = () => {
reject(new Error(xhr.statusText));
} }
}); else {
fail();
done();
}
};
xhr.onerror = function() {
fail();
done();
}
} }
}); });

View File

@ -55,6 +55,7 @@ namespace Microsoft.AspNetCore.Sockets
} }
else else
{ {
context.Response.ContentType = "text/plain";
context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed; context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed;
} }
} }
@ -78,6 +79,7 @@ namespace Microsoft.AspNetCore.Sockets
} }
else else
{ {
context.Response.ContentType = "text/plain";
context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed; context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed;
} }
} }
@ -167,6 +169,7 @@ namespace Microsoft.AspNetCore.Sockets
// The connection was disposed // The connection was disposed
context.Response.StatusCode = StatusCodes.Status404NotFound; context.Response.StatusCode = StatusCodes.Status404NotFound;
context.Response.ContentType = "plain/text";
return; return;
} }
@ -414,6 +417,8 @@ namespace Microsoft.AspNetCore.Sockets
return; return;
} }
context.Response.ContentType = "text/plain";
var transport = (TransportType?)connection.Metadata[ConnectionMetadataNames.Transport]; var transport = (TransportType?)connection.Metadata[ConnectionMetadataNames.Transport];
if (transport == TransportType.WebSockets) if (transport == TransportType.WebSockets)
{ {
@ -447,6 +452,7 @@ namespace Microsoft.AspNetCore.Sockets
{ {
if ((supportedTransports & transportType) == 0) if ((supportedTransports & transportType) == 0)
{ {
context.Response.ContentType = "text/plain";
context.Response.StatusCode = StatusCodes.Status404NotFound; context.Response.StatusCode = StatusCodes.Status404NotFound;
_logger.TransportNotSupported(connection.ConnectionId, transportType); _logger.TransportNotSupported(connection.ConnectionId, transportType);
await context.Response.WriteAsync($"{transportType} transport not supported by this end point type"); await context.Response.WriteAsync($"{transportType} transport not supported by this end point type");
@ -461,6 +467,7 @@ namespace Microsoft.AspNetCore.Sockets
} }
else if (transport != transportType) else if (transport != transportType)
{ {
context.Response.ContentType = "text/plain";
context.Response.StatusCode = StatusCodes.Status400BadRequest; context.Response.StatusCode = StatusCodes.Status400BadRequest;
_logger.CannotChangeTransport(connection.ConnectionId, transport.Value, transportType); _logger.CannotChangeTransport(connection.ConnectionId, transport.Value, transportType);
await context.Response.WriteAsync("Cannot change transports mid-connection"); await context.Response.WriteAsync("Cannot change transports mid-connection");
@ -495,6 +502,7 @@ namespace Microsoft.AspNetCore.Sockets
{ {
// There's no connection ID: bad request // There's no connection ID: bad request
context.Response.StatusCode = StatusCodes.Status400BadRequest; context.Response.StatusCode = StatusCodes.Status400BadRequest;
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("Connection ID required"); await context.Response.WriteAsync("Connection ID required");
return null; return null;
} }
@ -503,6 +511,7 @@ namespace Microsoft.AspNetCore.Sockets
{ {
// No connection with that ID: Not Found // No connection with that ID: Not Found
context.Response.StatusCode = StatusCodes.Status404NotFound; context.Response.StatusCode = StatusCodes.Status404NotFound;
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("No Connection with that ID"); await context.Response.WriteAsync("No Connection with that ID");
return null; return null;
} }

View File

@ -35,12 +35,11 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Transports
{ {
await _application.Completion; await _application.Completion;
_logger.LongPolling204(_connectionId, context.TraceIdentifier); _logger.LongPolling204(_connectionId, context.TraceIdentifier);
context.Response.ContentType = "text/plain";
context.Response.StatusCode = StatusCodes.Status204NoContent; context.Response.StatusCode = StatusCodes.Status204NoContent;
return; return;
} }
// REVIEW: What should the content type be?
var contentLength = 0; var contentLength = 0;
var buffers = new List<byte[]>(); var buffers = new List<byte[]>();
// We're intentionally not checking cancellation here because we need to drain messages we've got so far, // We're intentionally not checking cancellation here because we need to drain messages we've got so far,
@ -54,6 +53,7 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Transports
} }
context.Response.ContentLength = contentLength; context.Response.ContentLength = contentLength;
context.Response.ContentType = "application/octet-stream";
foreach (var buffer in buffers) foreach (var buffer in buffers)
{ {
@ -80,12 +80,14 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Transports
_logger.PollTimedOut(_connectionId, context.TraceIdentifier); _logger.PollTimedOut(_connectionId, context.TraceIdentifier);
context.Response.ContentLength = 0; context.Response.ContentLength = 0;
context.Response.ContentType = "text/plain";
context.Response.StatusCode = StatusCodes.Status200OK; context.Response.StatusCode = StatusCodes.Status200OK;
} }
else else
{ {
// Case 3 // Case 3
_logger.LongPolling204(_connectionId, context.TraceIdentifier); _logger.LongPolling204(_connectionId, context.TraceIdentifier);
context.Response.ContentType = "text/plain";
context.Response.StatusCode = StatusCodes.Status204NoContent; context.Response.StatusCode = StatusCodes.Status204NoContent;
} }
} }