/
CustomRequestDataTests.cs
169 lines (147 loc) · 5.13 KB
/
CustomRequestDataTests.cs
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
using System;
using System.IO;
using System.Net;
using System.Web;
using NUnit.Framework;
using ServiceStack.Common;
using ServiceStack.Common.Web;
using ServiceStack.ServiceClient.Web;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints.Tests.Support.Host;
using ServiceStack.WebHost.Endpoints.Tests.Support.Operations;
namespace ServiceStack.WebHost.Endpoints.Tests
{
[TestFixture]
public class CustomRequestDataTests
{
private const string ListeningOn = "http://localhost:82/";
ExampleAppHostHttpListener appHost;
readonly JsonServiceClient client = new JsonServiceClient(ListeningOn);
private string customUrl = ListeningOn.CombineWith("customrequestbinder");
private string predefinedUrl = ListeningOn.CombineWith("json/syncreply/customrequestbinder");
[TestFixtureSetUp]
public void OnTestFixtureSetUp()
{
appHost = new ExampleAppHostHttpListener();
appHost.Init();
appHost.Start(ListeningOn);
}
[TestFixtureTearDown]
public void OnTestFixtureTearDown()
{
appHost.Dispose();
}
/// <summary>
/// first-name=tom&item-0=blah&item-1-delete=1
/// </summary>
[Test]
public void Can_parse_custom_form_data()
{
var webReq = (HttpWebRequest)WebRequest.Create("http://localhost:82/customformdata?format=json");
webReq.Method = HttpMethods.Post;
webReq.ContentType = ContentType.FormUrlEncoded;
try
{
using (var sw = new StreamWriter(webReq.GetRequestStream()))
{
sw.Write("&first-name=tom&item-0=blah&item-1-delete=1");
}
var response = new StreamReader(webReq.GetResponse().GetResponseStream()).ReadToEnd();
Assert.That(response, Is.EqualTo("{\"FirstName\":\"tom\",\"Item0\":\"blah\",\"Item1Delete\":\"1\"}"));
}
catch (WebException webEx)
{
var errorWebResponse = ((HttpWebResponse)webEx.Response);
var errorResponse = new StreamReader(errorWebResponse.GetResponseStream()).ReadToEnd();
Assert.Fail(errorResponse);
}
}
[Test]
public void Does_use_request_binder_for_GET()
{
var response = client.Get<CustomRequestBinderResponse>("/customrequestbinder");
Assert.That(response.FromBinder);
}
[Test]
public void Does_use_request_binder_for_predefined_GET()
{
var responseStr = predefinedUrl.GetJsonFromUrl();
Console.WriteLine(responseStr);
var response = responseStr.FromJson<CustomRequestBinderResponse>();
Assert.That(response.FromBinder);
}
[Test]
public void Does_use_request_binder_for_predefined_GET_with_QueryString()
{
var customUrlWithQueryString = customUrl + "?IsFromBinder=false";
var responseStr = customUrlWithQueryString.GetJsonFromUrl();
Console.WriteLine(responseStr);
var response = responseStr.FromJson<CustomRequestBinderResponse>();
Assert.That(response.FromBinder);
}
[Test]
public void Does_use_request_binder_for_Send()
{
var response = client.Send<CustomRequestBinderResponse>(new CustomRequestBinder());
Assert.That(response.FromBinder);
}
[Test]
public void Does_use_request_binder_for_POST()
{
var response = client.Post<CustomRequestBinderResponse>("/customrequestbinder", new CustomRequestBinder());
Assert.That(response.FromBinder);
}
[Test]
public void Does_use_request_binder_for_POST_FormData()
{
var responseStr = customUrl.PostToUrl("IsFromBinder=false", acceptContentType:ContentType.Json);
Console.WriteLine(responseStr);
var response = responseStr.FromJson<CustomRequestBinderResponse>();
Assert.That(response.FromBinder);
}
[Test]
public void Does_use_request_binder_for_POST_FormData_without_ContentType()
{
var responseStr = customUrl.PostJsonToUrl("{\"IsFromBinder\":false}");
Console.WriteLine(responseStr);
var response = responseStr.FromJson<CustomRequestBinderResponse>();
Assert.That(response.FromBinder);
}
[Test]
public void Does_use_request_binder_for_POST_FormData_without_ContentType_with_QueryString()
{
string customUrlWithQueryString = customUrl + "?IsFromBinder=false";
var responseStr = customUrlWithQueryString.PostToUrl("k=v", acceptContentType: ContentType.Json);
var response = responseStr.FromJson<CustomRequestBinderResponse>();
Assert.That(response.FromBinder);
}
[Test]
public void Does_use_request_binder_for_predefined_POST()
{
var responseStr = predefinedUrl.PostJsonToUrl(new CustomRequestBinder());
Console.WriteLine(responseStr);
var response = responseStr.FromJson<CustomRequestBinderResponse>();
Assert.That(response.FromBinder);
}
[Test]
public void Does_use_request_binder_for_predefined_POST_FormData()
{
var responseStr = predefinedUrl.PostToUrl("k=v", acceptContentType: ContentType.Json);
Console.WriteLine(responseStr);
var response = responseStr.FromJson<CustomRequestBinderResponse>();
Assert.That(response.FromBinder);
}
[Test]
public void Does_use_request_binder_for_PUT()
{
var response = client.Put<CustomRequestBinderResponse>("/customrequestbinder", new CustomRequestBinder());
Assert.That(response.FromBinder);
}
[Test]
public void Does_use_request_binder_for_DELETE()
{
var response = client.Delete<CustomRequestBinderResponse>("/customrequestbinder");
Assert.That(response.FromBinder);
}
}
}