forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueRequestTests.cs
More file actions
142 lines (119 loc) · 4.5 KB
/
Copy pathUniqueRequestTests.cs
File metadata and controls
142 lines (119 loc) · 4.5 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
using System.Collections.Generic;
using System.IO;
using Funq;
using NUnit.Framework;
using ServiceStack.Common;
using ServiceStack.MiniProfiler.UI;
using ServiceStack.Text;
namespace ServiceStack.WebHost.Endpoints.Tests
{
[Route("/request/{Id}")]
public class ById
{
public string Id { get; set; }
}
[Route("/collections")]
public class Collections : IReturn<Collections>
{
public int[] Ids { get; set; }
public List<string> Names { get; set; }
}
public class UniqueRequestService : IService
{
public string Get(ById byId)
{
return byId.Id;
}
public object Any(Collections request)
{
return request;
}
}
public class UniqueRequestAppHost : AppHostHttpListenerBase
{
public UniqueRequestAppHost() : base("Unique Request Tests", typeof(UniqueRequestService).Assembly) {}
public override void Configure(Container container) {}
}
[TestFixture]
public class UniqueRequestTests
{
private const string BaseUri = "http://localhost:8000";
private UniqueRequestAppHost appHost;
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
appHost = new UniqueRequestAppHost();
appHost.Init();
appHost.Start(BaseUri + "/");
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
appHost.Dispose();
}
[Test]
public void Can_handle_encoded_chars()
{
var response = BaseUri.CombineWith("request/123%20456").GetStringFromUrl();
Assert.That(response, Is.EqualTo("123 456"));
response = BaseUri.CombineWith("request/123%7C456").GetStringFromUrl();
Assert.That(response, Is.EqualTo("123|456"));
}
[Test]
public void Can_handle_collections_with_ServiceClient()
{
var client = new JsonServiceClient(BaseUri);
var request = new Collections {
Ids = new[] {1, 2, 3},
Names = new List<string> {"A", "B", "C"},
};
var response = client.Get(request);
Assert.That(response.Ids, Is.EquivalentTo(request.Ids));
Assert.That(response.Names, Is.EquivalentTo(request.Names));
}
[Test]
public void Can_handle_collections_with_HttpClient()
{
var url = BaseUri.CombineWith("collections")
.AddQueryParam("Ids", "1,2,3")
.AddQueryParam("Names", "A,B,C");
var response = url.GetJsonFromUrl()
.FromJson<Collections>();
Assert.That(response.Ids, Is.EquivalentTo(new[] { 1, 2, 3 }));
Assert.That(response.Names, Is.EquivalentTo(new List<string> { "A", "B", "C" }));
url = BaseUri.CombineWith("collections")
.AddQueryParam("Ids", "1")
.AddQueryParam("Ids", "2")
.AddQueryParam("Ids", "3")
.AddQueryParam("Names", "A")
.AddQueryParam("Names", "B")
.AddQueryParam("Names", "C");
response = url.GetJsonFromUrl()
.FromJson<Collections>();
Assert.That(response.Ids, Is.EquivalentTo(new[] { 1, 2, 3 }));
Assert.That(response.Names, Is.EquivalentTo(new List<string> { "A", "B", "C" }));
}
[Test]
public void Can_handle_collections_with_HttpClient_on_predefined_route()
{
var url = BaseUri.CombineWith("json/reply/Collections")
.AddQueryParam("Ids", "1,2,3")
.AddQueryParam("Names", "A,B,C");
var response = url.GetJsonFromUrl()
.FromJson<Collections>();
Assert.That(response.Ids, Is.EquivalentTo(new[] { 1, 2, 3 }));
Assert.That(response.Names, Is.EquivalentTo(new List<string> { "A", "B", "C" }));
url = BaseUri.CombineWith("json/reply/Collections")
.AddQueryParam("Ids", "1")
.AddQueryParam("Ids", "2")
.AddQueryParam("Ids", "3")
.AddQueryParam("Names", "A")
.AddQueryParam("Names", "B")
.AddQueryParam("Names", "C");
response = url.GetJsonFromUrl()
.FromJson<Collections>();
Assert.That(response.Ids, Is.EquivalentTo(new[] { 1, 2, 3 }));
Assert.That(response.Names, Is.EquivalentTo(new List<string> { "A", "B", "C" }));
}
}
}