108 lines
4.1 KiB
C#
108 lines
4.1 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Http.Internal;
|
|
using Microsoft.AspNetCore.Sockets.Internal;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Primitives;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.AspNetCore.Sockets.Tests
|
|
{
|
|
public class HttpConnectionDispatcherTests
|
|
{
|
|
[Fact]
|
|
public async Task NegotiateReservesConnectionIdAndReturnsIt()
|
|
{
|
|
var manager = new ConnectionManager();
|
|
var dispatcher = new HttpConnectionDispatcher(manager, new LoggerFactory());
|
|
var context = new DefaultHttpContext();
|
|
var services = new ServiceCollection();
|
|
services.AddSingleton<TestEndPoint>();
|
|
context.RequestServices = services.BuildServiceProvider();
|
|
var ms = new MemoryStream();
|
|
context.Request.Path = "/negotiate";
|
|
context.Response.Body = ms;
|
|
await dispatcher.ExecuteAsync<TestEndPoint>("", context);
|
|
|
|
var id = Encoding.UTF8.GetString(ms.ToArray());
|
|
|
|
ConnectionState state;
|
|
Assert.True(manager.TryGetConnection(id, out state));
|
|
Assert.Equal(id, state.Connection.ConnectionId);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("/send")]
|
|
[InlineData("/sse")]
|
|
[InlineData("/poll")]
|
|
[InlineData("/ws")]
|
|
public async Task EndpointsThatAcceptConnectionId404WhenUnknownConnectionIdProvided(string path)
|
|
{
|
|
var manager = new ConnectionManager();
|
|
var dispatcher = new HttpConnectionDispatcher(manager, new LoggerFactory());
|
|
|
|
using (var strm = new MemoryStream())
|
|
{
|
|
var context = new DefaultHttpContext();
|
|
context.Response.Body = strm;
|
|
|
|
var services = new ServiceCollection();
|
|
services.AddSingleton<TestEndPoint>();
|
|
context.RequestServices = services.BuildServiceProvider();
|
|
context.Request.Path = path;
|
|
var values = new Dictionary<string, StringValues>();
|
|
values["id"] = "unknown";
|
|
var qs = new QueryCollection(values);
|
|
context.Request.Query = qs;
|
|
|
|
await dispatcher.ExecuteAsync<TestEndPoint>("", context);
|
|
|
|
Assert.Equal(StatusCodes.Status404NotFound, context.Response.StatusCode);
|
|
await strm.FlushAsync();
|
|
Assert.Equal("No Connection with that ID", Encoding.UTF8.GetString(strm.ToArray()));
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("/send")]
|
|
[InlineData("/sse")]
|
|
[InlineData("/poll")]
|
|
public async Task EndpointsThatRequireConnectionId400WhenNoConnectionIdProvided(string path)
|
|
{
|
|
var manager = new ConnectionManager();
|
|
var dispatcher = new HttpConnectionDispatcher(manager, new LoggerFactory());
|
|
using (var strm = new MemoryStream())
|
|
{
|
|
var context = new DefaultHttpContext();
|
|
context.Response.Body = strm;
|
|
var services = new ServiceCollection();
|
|
services.AddSingleton<TestEndPoint>();
|
|
context.RequestServices = services.BuildServiceProvider();
|
|
context.Request.Path = path;
|
|
|
|
await dispatcher.ExecuteAsync<TestEndPoint>("", context);
|
|
|
|
Assert.Equal(StatusCodes.Status400BadRequest, context.Response.StatusCode);
|
|
await strm.FlushAsync();
|
|
Assert.Equal("Connection ID required", Encoding.UTF8.GetString(strm.ToArray()));
|
|
}
|
|
}
|
|
}
|
|
|
|
public class TestEndPoint : EndPoint
|
|
{
|
|
public override Task OnConnectedAsync(Connection connection)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|