File size: 8,072 Bytes
7b715bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | /*
Copyright 2017 Google Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace Google.Apis.Tests.Apis.Upload
{
public partial class ResumableUploadTest
{
/// <summary>
/// HTTP server which listens on localhost:<random port> for testing.
/// </summary>
/// <remarks>
/// <para>A single server is started for all tests, and is shutdown when all tests
/// have run.</para>
/// <para>Each test registers its own <see cref="Handler"/> with a unique
/// URL path prefix, with the handler designed to fake a server with the
/// required behaviour for the specific test. The test handler is unregistered
/// when the test ends.</para>
/// </remarks>
private class TestServer : IDisposable
{
private readonly HttpListener _httpListener;
private readonly Task _httpTask;
private int _requestCounter;
public string HttpPrefix { get; }
internal TestLogger Logger { get; }
public TestServer(TestLogger logger)
{
Logger = logger;
var rnd = new Random();
// Find an available port and start an HttpListener.
do
{
_httpListener = new HttpListener();
HttpPrefix = $"http://localhost:{rnd.Next(49152, 65535)}/";
_httpListener.Prefixes.Add(HttpPrefix);
try
{
_httpListener.Start();
}
// Catch errors that mean the port is already in use
catch (HttpListenerException e) when (e.ErrorCode == 183 || e.ErrorCode == 32 || e.Message.Contains("already in use"))
{
_httpListener.Close();
_httpListener = null;
}
catch (SocketException e) when (e.SocketErrorCode == SocketError.AddressAlreadyInUse)
{
_httpListener.Close();
_httpListener = null;
}
} while (_httpListener == null);
_httpTask = RunServer();
}
private async Task RunServer()
{
while (_httpListener.IsListening)
{
int requestId = Interlocked.Increment(ref _requestCounter);
var context = await _httpListener.GetContextAsync();
Logger.WriteLine($"HttpListener received request {requestId} with path {context.Request.Url.AbsolutePath}");
var response = context.Response;
if (context.Request.Url.AbsolutePath.EndsWith("/Quit", StringComparison.Ordinal))
{
response.Close();
_httpListener.Stop();
}
else
{
response.ContentType = "text/plain";
IEnumerable<byte> body;
try
{
body = await HandleCall(context.Request, response);
var bodyBytes = body?.ToArray() ?? new byte[0];
if (bodyBytes.Length > 0)
{
await response.OutputStream.WriteAsync(bodyBytes, 0, bodyBytes.Length);
}
Logger.WriteLine($"Request {requestId} completed with status code: {response.StatusCode}; content length {bodyBytes.Length}");
}
catch (HttpListenerException ex)
{
Logger.WriteLine($"HttpListener failed while handling response for request {requestId}: {ex}");
}
finally
{
try
{
response.Close();
}
catch (Exception ex)
{
Logger.WriteLine($"HttpListener failed while closing response for request {requestId}: {ex}");
}
}
}
}
}
public abstract class Handler : IDisposable
{
private static int handlerId = 0;
private TestServer _server;
public Handler(TestServer server)
{
_server = server;
Id = Interlocked.Increment(ref handlerId).ToString();
_server.RegisterHandler(this);
}
public string Id { get; }
public string HttpPrefix => $"{_server.HttpPrefix}{Id}/";
public string RemovePrefix(string s)
{
var prefix = $"/{Id}/";
if (s.StartsWith(prefix, StringComparison.Ordinal))
{
return s.Substring(prefix.Length);
}
throw new InvalidOperationException("Doesn't start with prefix");
}
public List<RequestInfo> Requests { get; } = new List<RequestInfo>();
public Task<IEnumerable<byte>> HandleCall0(
HttpListenerRequest request, HttpListenerResponse response)
{
Requests.Add(new RequestInfo(request));
return HandleCall(request, response);
}
protected abstract Task<IEnumerable<byte>> HandleCall(
HttpListenerRequest request, HttpListenerResponse response);
public void Dispose()
{
_server.UnregisterHandler(Id);
}
}
private ConcurrentDictionary<string, Handler> _handlers =
new ConcurrentDictionary<string, Handler>();
private Task<IEnumerable<byte>> HandleCall(
HttpListenerRequest request, HttpListenerResponse response)
{
var id = request.Url.Segments[1].TrimEnd('/');
var handler = _handlers[id];
return handler.HandleCall0(request, response);
}
private void RegisterHandler(Handler handler)
{
_handlers.TryAdd(handler.Id, handler);
}
private void UnregisterHandler(string id)
{
Handler handler;
_handlers.TryRemove(id, out handler);
}
public void Dispose()
{
var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5));
_httpTask.ContinueWith(task => timeout.Cancel());
Task.Run(async () =>
{
await new HttpClient().GetAsync(HttpPrefix + "Quit", timeout.Token);
_httpListener.Stop();
});
_httpTask.Wait();
}
}
}
}
|