This repository was archived by the owner on Jun 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathUsingRawHttpClient.cs
More file actions
152 lines (131 loc) · 5.33 KB
/
UsingRawHttpClient.cs
File metadata and controls
152 lines (131 loc) · 5.33 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.IO;
using System.Linq;
using System.Net;
using System.Xml.Linq;
using NUnit.Framework;
using Sakila.ServiceModel.Version100.Operations.SakilaService;
using ServiceStack.ServiceModel.Extensions;
using ServiceStack.ServiceModel.Serialization;
using ServiceStack.UsageExamples.Support;
namespace ServiceStack.UsageExamples
{
[TestFixture]
public class UsingRawHttpClient : TestBase
{
[Test]
public void Get_customers_using_soap12_http_post()
{
var soapRequest =
@"<s:Envelope xmlns:s=""http://www.w3.org/2003/05/soap-envelope"" xmlns:a=""http://www.w3.org/2005/08/addressing"">
<s:Header>
<a:Action s:mustUnderstand=""1"">http://services.servicestack.net/GetCustomers</a:Action>
<a:To s:mustUnderstand=""1"">{0}</a:To>
</s:Header>
<s:Body>
<GetCustomers xmlns=""http://schemas.servicestack.net/types/"">
<CustomerIds>
<Id>{1}</Id>
</CustomerIds>
<Version>100</Version>
</GetCustomers>
</s:Body>
</s:Envelope>";
var request = string.Format(soapRequest, WsSyncReplyUri, CustomerId);
var client = (HttpWebRequest)WebRequest.Create(base.WsSyncReplyUri);
client.ContentType = "application/soap+xml; charset=utf-8";
client.Accept = "text/xml";
client.Method = "POST";
using (var stream = client.GetRequestStream())
{
using (var writer = new StreamWriter(stream))
{
writer.Write(request);
}
}
var soapResponse = new StreamReader(client.GetResponse().GetResponseStream()).ReadToEnd();
var el = XElement.Parse(soapResponse);
var customers = el.AnyElement("Body").AnyElement("GetCustomersResponse")
.AnyElement("Customers").AllElements("Customer").ToList();
Assert.AreEqual(1, customers.Count);
Assert.AreEqual(CustomerId, customers[0].GetInt("Id"));
}
[Test]
public void Get_customers_using_soap11_http_post()
{
var soapRequest =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<GetCustomers xmlns=""http://schemas.servicestack.net/types/"">
<CustomerIds>
<Id>{0}</Id>
</CustomerIds>
<Version>100</Version>
</GetCustomers>
</soap:Body>
</soap:Envelope>";
var request = string.Format(soapRequest, CustomerId);
var client = (HttpWebRequest)WebRequest.Create(base.BasicHttpSyncReplyUri);
client.ContentType = "text/xml; charset=utf-8";
client.Headers.Add("SOAPAction", "GetCustomers");
client.Accept = "text/xml";
client.Method = "POST";
using (var stream = client.GetRequestStream())
{
using (var writer = new StreamWriter(stream))
{
writer.Write(request);
}
}
var soapResponse = new StreamReader(client.GetResponse().GetResponseStream()).ReadToEnd();
var el = XElement.Parse(soapResponse);
var customers = el.AnyElement("Body").AnyElement("GetCustomersResponse")
.AnyElement("Customers").AllElements("Customer").ToList();
Assert.AreEqual(1, customers.Count);
Assert.AreEqual(CustomerId, customers[0].GetInt("Id"));
}
[Test]
public void Get_customers_using_xml_http_post()
{
var xmlRequest = string.Format(
@"<GetCustomers xmlns=""http://schemas.servicestack.net/types/"">
<CustomerIds>
<Id>{0}</Id>
</CustomerIds>
<Version>100</Version>
</GetCustomers>", CustomerId);
var requestUri = base.XmlSyncReplyBaseUri + "/GetCustomers";
var client = WebRequest.Create(requestUri);
client.Method = "POST";
client.ContentType = "application/xml";
using (var writer = new StreamWriter(client.GetRequestStream()))
{
writer.Write(xmlRequest);
}
var xmlResponse = new StreamReader(client.GetResponse().GetResponseStream()).ReadToEnd();
var el = XElement.Parse(xmlResponse);
var customers = el.AnyElement("Customers").AllElements("Customer").ToList();
Assert.AreEqual(1, customers.Count);
Assert.AreEqual(CustomerId, customers[0].GetInt("Id"));
}
[Test]
public void Get_customers_using_json_http_post()
{
var jsonRequest = string.Format(@"{{""CustomerIds"":[{0}],""Version"":0}}", CustomerId);
var requestUri = base.JsonSyncReplyBaseUri + "/GetCustomers";
var client = WebRequest.Create(requestUri);
client.Method = "POST";
client.ContentType = "application/json";
using (var writer = new StreamWriter(client.GetRequestStream()))
{
writer.Write(jsonRequest);
}
var jsonResponse = new StreamReader(client.GetResponse().GetResponseStream()).ReadToEnd();
var response = JsonDataContractDeserializer.Instance.Parse(jsonResponse,
typeof(GetCustomersResponse)) as GetCustomersResponse;
Assert.IsNotNull(response);
Assert.AreEqual(1, response.Customers.Count);
Assert.AreEqual(CustomerId, response.Customers[0].Id);
}
}
}