Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 176 lines (159 sloc) 6.049 kB
4a6c5e5 @desunit Normalize line endings
desunit authored
1 using System;
2 using System.Linq;
3 using System.Collections.Generic;
4 using NUnit.Framework;
5
6 namespace ServiceStack.Text.Tests.UseCases
7 {
8 [TestFixture]
9 public class GitHubRestTests
10 {
87ef43a @mythz Added convenience methon JsonObject for parsing 3rd party JSON Apis.
mythz authored
11 private const string JsonGitResponse = @"{
d5f8735 @mythz Add test for GitHub pull rest api
mythz authored
12 ""pulls"": [
13 {
14 ""state"": ""open"",
15 ""base"": {
16 ""label"": ""technoweenie:master"",
17 ""ref"": ""master"",
18 ""sha"": ""53397635da83a2f4b5e862b5e59cc66f6c39f9c6"",
19 },
20 ""head"": {
21 ""label"": ""smparkes:synchrony"",
22 ""ref"": ""synchrony"",
23 ""sha"": ""83306eef49667549efebb880096cb539bd436560"",
24 },
25 ""discussion"": [
26 {
27 ""type"": ""IssueComment"",
28 ""gravatar_id"": ""821395fe70906c8290df7f18ac4ac6cf"",
29 ""created_at"": ""2010/10/07 07:38:35 -0700"",
30 ""body"": ""Did you intend to remove net/http? Otherwise, this looks good. Have you tried running the LIVE tests with it?\r\n\r\n ruby test/live_server.rb # start the demo server\r\n LIVE=1 rake"",
31 ""updated_at"": ""2010/10/07 07:38:35 -0700"",
32 ""id"": 453980,
33 },
34 {
35 ""type"": ""Commit"",
36 ""created_at"": ""2010-11-04T16:27:45-07:00"",
37 ""sha"": ""83306eef49667549efebb880096cb539bd436560"",
38 ""author"": ""Steven Parkes"",
39 ""subject"": ""add em_synchrony support"",
40 ""email"": ""smparkes@smparkes.net""
41 }
42 ],
43 ""title"": ""Synchrony"",
44 ""body"": ""Here's the pull request.\r\n\r\nThis isn't generic EM: require's Ilya's synchrony and needs to be run on its own fiber, e.g., via synchrony or rack-fiberpool.\r\n\r\nI thought about a \""first class\"" em adapter, but I think the faraday api is sync right now, right? Interesting idea to add something like rack's async support to faraday, but that's an itch I don't have right now."",
45 ""position"": 4.0,
46 ""number"": 15,
47 ""votes"": 0,
48 ""comments"": 4,
49 ""diff_url"": ""https://github.com/technoweenie/faraday/pull/15.diff"",
50 ""patch_url"": ""https://github.com/technoweenie/faraday/pull/15.patch"",
51 ""labels"": [],
52 ""html_url"": ""https://github.com/technoweenie/faraday/pull/15"",
53 ""issue_created_at"": ""2010-10-04T12:39:18-07:00"",
54 ""issue_updated_at"": ""2010-11-04T16:35:04-07:00"",
55 ""created_at"": ""2010-10-04T12:39:18-07:00"",
56 ""updated_at"": ""2010-11-04T16:30:14-07:00""
57 }
58 ]
4a6c5e5 @desunit Normalize line endings
desunit authored
59 }";
60
61 public class Discussion
62 {
63 public string Type { get; set; }
64 public string GravatarId { get; set; }
65 public string CreatedAt { get; set; }
66 public string Body { get; set; }
67 public string UpdatedAt { get; set; }
68
69 public int? Id { get; set; }
70 public string Sha { get; set; }
71 public string Author { get; set; }
72 public string Subject { get; set; }
73 public string Email { get; set; }
74 }
75
76 [Test]
77 public void Can_Parse_Discussion_using_JsonObject()
78 {
79 List<Discussion> discussions = JsonObject.Parse(JsonGitResponse)
80 .ArrayObjects("pulls")[0]
81 .ArrayObjects("discussion")
82 .ConvertAll(x => new Discussion
83 {
84 Type = x.Get("type"),
85 GravatarId = x.Get("gravatar_id"),
86 CreatedAt = x.Get("created_at"),
87 Body = x.Get("body"),
88 UpdatedAt = x.Get("updated_at"),
89
90 Id = x.JsonTo<int?>("id"),
91 Sha = x.Get("sha"),
92 Author = x.Get("author"),
93 Subject = x.Get("subject"),
94 Email = x.Get("email"),
95 });
96
97 Console.WriteLine(discussions.Dump()); //See what's been parsed
98 Assert.That(discussions.ConvertAll(x => x.Type), Is.EquivalentTo(new[] { "IssueComment", "Commit" }));
99 }
100
101 [Test]
102 public void Can_Parse_Discussion_using_only_NET_collection_classes()
103 {
104 var jsonObj = JsonSerializer.DeserializeFromString<List<JsonObject>>(JsonGitResponse);
105 var jsonPulls = JsonSerializer.DeserializeFromString<List<JsonObject>>(jsonObj[0].Child("pulls"));
106 var discussions = JsonSerializer.DeserializeFromString<List<JsonObject>>(jsonPulls[0].Child("discussion"))
107 .ConvertAll(x => new Discussion
108 {
109 Type = x.Get("type"),
110 GravatarId = x.Get("gravatar_id"),
111 CreatedAt = x.Get("created_at"),
112 Body = x.Get("body"),
113 UpdatedAt = x.Get("updated_at"),
114
115 Id = x.JsonTo<int?>("id"),
116 Sha = x.Get("sha"),
117 Author = x.Get("author"),
118 Subject = x.Get("subject"),
119 Email = x.Get("email"),
120 });
121
122 Console.WriteLine(discussions.Dump()); //See what's been parsed
123 Assert.That(discussions.ConvertAll(x => x.Type), Is.EquivalentTo(new[] { "IssueComment", "Commit" }));
124 }
125
126
127 public class GitHubResponse
128 {
129 public List<Pull> pulls { get; set; }
130 }
131
132 public class Pull
133 {
134 public List<discussion> discussion { get; set; }
135 public string title { get; set; }
136 public string body { get; set; }
137 public double position { get; set; }
138 public int number { get; set; }
139 public int votes { get; set; }
140 public int comments { get; set; }
141 public string diff_url { get; set; }
142 public string patch_url { get; set; }
143 public string html_url { get; set; }
144 public DateTime issue_created_date { get; set; }
145 public DateTime issue_updated_at { get; set; }
146 public DateTime created_at { get; set; }
147 public DateTime updated_at { get; set; }
148 }
149
150 public class discussion
151 {
152 public string type { get; set; }
153 public string gravatar_id { get; set; }
154 public string created_at { get; set; }
155 public string body { get; set; }
156 public string updated_at { get; set; }
157
158 public int? id { get; set; }
159 public string sha { get; set; }
160 public string author { get; set; }
161 public string subject { get; set; }
162 public string email { get; set; }
163 }
164
165 [Test]
166 public void Can_Parse_Discussion_using_custom_client_DTOs()
167 {
168 var gitHubResponse = JsonSerializer.DeserializeFromString<GitHubResponse>(JsonGitResponse);
169
170 Console.WriteLine(gitHubResponse.Dump()); //See what's been parsed
60c583c @mythz re-sync SS dlls used in tests, copy over SS.Common.Tests models into …
mythz authored
171 Assert.That(gitHubResponse.pulls.SelectMany(p => p.discussion).Map(x => x.type),
4a6c5e5 @desunit Normalize line endings
desunit authored
172 Is.EquivalentTo(new[] { "IssueComment", "Commit" }));
173 }
174
175 }
d5f8735 @mythz Add test for GitHub pull rest api
mythz authored
176 }
Something went wrong with that request. Please try again.