-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHttpTransportTests.cs
More file actions
129 lines (110 loc) · 4.04 KB
/
HttpTransportTests.cs
File metadata and controls
129 lines (110 loc) · 4.04 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Moq;
using System.Xml.Linq;
using System.Net;
using System.IO;
using FluentAssertions;
namespace PSWinCom.Gateway.Client.Tests
{
[TestFixture]
public class HttpTransportTests
{
private HttpTransport transport;
[Test]
public void Should_pass_on_xml()
{
XDocument document = new XDocument(new XElement("TEST"));
with_listener(
at: "http://localhost:56123/listener/error/",
that_returns: 500,
with_body: new XDocument(document),
ensure: (transport, request) =>
{
transport
.Send(document)
.Success
.Should().BeFalse();
request
.Result
.Should().Be(expected_string_representation_of(document));
}
);
}
[Test]
public void Should_return_success_when_status_code_is_200()
{
with_listener(at: "http://localhost:56123/listener/ok/",
that_returns: 200,
with_body: new XDocument(new XElement("ROOT")),
ensure: (transport, server) =>
{
transport
.Send(new XDocument(new XElement("TEST")))
.Success
.Should().BeTrue();
}
);
}
[Test]
public void Should_return_response_content_of_successful_request()
{
with_listener(at: "http://localhost:56123/listener/ok/",
that_returns: 200,
with_body: new XDocument(new XElement("ROOT")),
ensure: (transport, server) =>
{
transport
.Send(new XDocument(new XElement("TEST")))
.Content.Root.Name.Should().Be("ROOT");
}
);
}
private static void with_listener(string at, int that_returns, XDocument with_body, Action<HttpTransport, Task<string>> ensure) {
var endpoint = new Uri(at);
Task<string> server = listener(
at: at,
that_returns: that_returns,
with_body: with_body);
server.Start();
var transport = new HttpTransport(endpoint);
ensure(transport, server);
}
[SetUp]
public void Setup()
{
transport = new HttpTransport(new Uri("http://localhost:56123/listener/"));
}
private static Task<string> listener(string at, int that_returns, XDocument with_body)
{
var server = new Task<string>((s) =>
{
var state = (Tuple<int, XDocument>)s;
var listener = new HttpListener();
listener.Prefixes.Add(at);
listener.Start();
var context = listener.GetContext();
var request_body = new StreamReader(context.Request.InputStream).ReadToEnd();
context.Response.StatusCode = state.Item1;
state.Item2.Save(context.Response.OutputStream);
context.Response.Close();
return request_body;
}, new Tuple<int, XDocument>(that_returns, with_body));
return server;
}
private string expected_string_representation_of(XDocument document)
{
document.Declaration = new XDeclaration("1.0", "iso8859-1", null);
using (var expectedStream = new MemoryStream())
{
document.Save(expectedStream);
expectedStream.Seek(0, SeekOrigin.Begin);
return new StreamReader(expectedStream, Encoding.GetEncoding("iso8859-1")).ReadToEnd();
}
}
}
}