private static void SimulateSendingFailureOnFirstAttempt(string url, HttpStatusCode secondAttempt) { var onSecondAttempt = new ManualResetEvent(initialState: false); StubHttpServer.SimulateFailureOnFirstAttempt(url, secondAttempt, onSecondAttempt); Assert.True(onSecondAttempt.WaitOne(TimeSpan.FromMinutes(1))); }
private static async Task RespondToPullRequestAsync(string url, Action <HttpListenerResponse> response) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; var waiter = new ManualResetEvent(false); StubHttpServer.StartServer(url, response, waiter); waiter.WaitOne(timeout: TimeSpan.FromSeconds(10)); // Wait till the response is processed correctly. await Task.Delay(TimeSpan.FromSeconds(10)); }
public async Task CanSetupAndReturnAnHttpResponseUsingRelativeUri() { using (var server = new StubHttpServer()) using (var httpClient = new HttpClient()) { server.SetupRoute("/hello").Get().ReturnsStatusCode(HttpStatusCode.OK).WithTextContent("HELLO WORLD"); httpClient.BaseAddress = new Uri(server.Url); var response = await httpClient.GetAsync("/hello"); response.StatusCode.Should().Be(HttpStatusCode.OK); (await response.Content.ReadAsStringAsync()).Should().Be("HELLO WORLD"); } }
private async Task CorrectHandlingOnSynchronouslyReceivedMultiHopReceipt( bool actAsIntermediaryMsh, string receivePModeId, OutStatus expectedOutStatus, Operation expectedSignalOperation) { // Arrange SendingProcessingMode pmode = CreateMultihopPMode(StubListenLocation); UserMessage simpleUserMessage = CreateMultihopUserMessage(receivePModeId, pmode); AS4Message as4Message = AS4Message.Create(simpleUserMessage, pmode); var signal = new ManualResetEvent(false); var serializer = new SoapEnvelopeSerializer(); StubHttpServer.StartServer( StubListenLocation, res => { res.StatusCode = 200; res.ContentType = Constants.ContentTypes.Soap; var receipt = Receipt.CreateFor( $"receipt-{Guid.NewGuid()}", as4Message.FirstUserMessage, userMessageSendViaMultiHop: true); serializer.Serialize(AS4Message.Create(receipt), res.OutputStream); }, signal); // Act PutMessageToSend(as4Message, pmode, actAsIntermediaryMsh); // Assert signal.WaitOne(); OutMessage sentMessage = await PollUntilPresent( () => _databaseSpy.GetOutMessageFor(m => m.EbmsMessageId == simpleUserMessage.MessageId), timeout : TimeSpan.FromSeconds(10)); Assert.Equal(expectedOutStatus, sentMessage.Status.ToEnum <OutStatus>()); InMessage receivedMessage = await PollUntilPresent( () => _databaseSpy.GetInMessageFor(m => m.EbmsRefToMessageId == simpleUserMessage.MessageId), timeout : TimeSpan.FromSeconds(10)); Assert.Equal(MessageType.Receipt, receivedMessage.EbmsMessageType); Assert.Equal(expectedSignalOperation, receivedMessage.Operation); }
public async Task CanSetupAndReturnAnHttpResponseUsingAbsoluteUri() { using (var server = new StubHttpServer()) using (var httpClient = new HttpClient()) { server.SetupRoute("/hello").Get().ReturnsStatusCode(HttpStatusCode.OK).WithTextContent("HELLO WORLD"); var endpoint = $"{server.Url}" + "/hello"; //server.Url contains trailing slash var response = await httpClient.GetAsync(endpoint); response.StatusCode.Should().Be(HttpStatusCode.OK); (await response.Content.ReadAsStringAsync()).Should().Be("HELLO WORLD"); } }
private void TestReceiveNRReceiptWith(string ebmsMessageId, Func <int, int> selection) { SendingProcessingMode nrrPMode = VerifyNRReceiptsPMode(); X509Certificate2 cert = new StubCertificateRepository().GetStubCertificate(); AS4Message userMessage = SignedUserMessage(ebmsMessageId, nrrPMode, cert); AS4Message nrReceipt = SignedNRReceipt(cert, userMessage, selection); var waitHandle = new ManualResetEvent(initialState: false); StubHttpServer.StartServer(StubListenLocation, new AS4MessageResponseHandler(nrReceipt).WriteResponse, waitHandle); PutMessageToSend(userMessage, nrrPMode, actAsIntermediaryMsh: false); waitHandle.WaitOne(); }
private async Task <IEnumerable <AS4Message> > RespondToPullRequestAsync(string url, int responseStatusCode) { var inputs = new Collection <Stream>(); var waiter = new ManualResetEvent(false); StubHttpServer.StartServerLifetime( url, (req, res) => { var input = new VirtualStream(); req.InputStream.CopyTo(input); inputs.Add(input); res.StatusCode = responseStatusCode; res.OutputStream.Dispose(); return(inputs.Count == 3 ? ServerLifetime.Stop : ServerLifetime.Continue); }, waiter); waiter.WaitOne(timeout: TimeSpan.FromSeconds(25)); // Wait till the response is processed correctly. await Task.Delay(TimeSpan.FromSeconds(2)); _as4Msh.Dispose(); var results = new Collection <AS4Message>(); foreach (Stream input in inputs) { input.Position = 0; AS4Message result = await SerializerProvider .Default .Get(Constants.ContentTypes.Soap) .DeserializeAsync(input, Constants.ContentTypes.Soap, CancellationToken.None); Assert.True(result != null, "PullRequest couldn't be deserialized"); results.Add(result); input.Dispose(); } return(results.AsEnumerable()); }
public void NoExceptionsAreLoggedWhenPullSenderIsNotAvailable() { // Arrange string pullSenderUrl = RetrievePullingUrlFromConfig(); _databaseSpy.ClearDatabase(); // Act var waiter = new ManualResetEvent(false); StubHttpServer.StartServer(pullSenderUrl, _ => throw new InvalidOperationException(), waiter); waiter.WaitOne(timeout: TimeSpan.FromSeconds(5)); // Assert _as4Msh.Dispose(); Assert.Empty(_databaseSpy.GetInExceptions(r => true)); }
public async Task CanSaveRepresentationOfIncomingRequests() { using (var server = new StubHttpServer()) using (var httpClient = new HttpClient()) { server.SetupRoute("/hello").Post().ReturnsStatusCode(HttpStatusCode.OK).WithTextContent("HELLO WORLD"); var endpoint = $"{server.Url}" + "/hello"; //server.Url contains trailing slash await httpClient.PostAsync(endpoint, new StringContent("HELLO", Encoding.UTF8, "text/plain")); var request = server.RequestLogs.First(); request.Method.Should().Be("POST"); request.Body.Should().Be("HELLO"); request.Url.ToString().Should().Be(endpoint); request.Headers["Content-Type"].Should().Be("text/plain; charset=utf-8"); } }
public async Task CanInvokeCallbackWhenRouteIsInvoked() { using (var server = new StubHttpServer()) using (var httpClient = new HttpClient()) { var wasCalled = false; server.SetupRoute("/hello") .Get() .ReturnsStatusCode(HttpStatusCode.OK) .WhenInvoked(ctx => wasCalled = true) .WithTextContent("HELLO WORLD"); httpClient.BaseAddress = new Uri(server.Url); var response = await httpClient.GetAsync("/hello"); response.StatusCode.Should().Be(HttpStatusCode.OK); (await response.Content.ReadAsStringAsync()).Should().Be("HELLO WORLD"); Assert.True(wasCalled, "expect callback to have been invoked."); } }
public async Task ResetsTheBodyStreamAfterReading() { using (var server = new StubHttpServer()) using (var httpClient = new HttpClient()) { Stream body = new MemoryStream(); server.SetupRoute("/hello") .Post() .ReturnsStatusCode(HttpStatusCode.OK) .WhenInvoked(ctx => ctx.Request.Body.CopyTo(body)) .WithTextContent("HELLO WORLD"); var endpoint = $"{server.Url}" + "/hello"; //server.Url contains trailing slash await httpClient.PostAsync(endpoint, new StringContent("HELLO", Encoding.UTF8, "text/plain")); using (var reader = new StreamReader(body)) { body.Position = 0; (await reader.ReadToEndAsync()).Should().Be("HELLO"); } } }
public DemoTest() { _server = new StubHttpServer(); }