Skip to content

Commit 4ba45d7

Browse files
authored
Adding tests for GrpcMessageExtensionUtilities (#11040)
* Adding tests for GrpcMessageExtensionUtilities.ConvertFromHttpMessageToExpando * add file header
1 parent 32c0e71 commit 4ba45d7

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Net;
8+
using System.Text;
9+
using System.Text.Json;
10+
using System.Threading.Tasks;
11+
using Google.Protobuf;
12+
using Google.Protobuf.Collections;
13+
using Microsoft.Azure.WebJobs.Script.Grpc;
14+
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
15+
using Xunit;
16+
17+
namespace Microsoft.Azure.WebJobs.Script.Tests
18+
{
19+
public sealed class GrpcMessageExtensionUtilitiesTests
20+
{
21+
[Theory]
22+
[InlineData(HttpStatusCode.OK)]
23+
[InlineData(HttpStatusCode.BadRequest)]
24+
[InlineData(HttpStatusCode.Accepted)]
25+
[InlineData(HttpStatusCode.NotFound)]
26+
public async Task ConvertFromHttpMessageToExpando_ShouldReturnExpandoObject_WithStringResponseAndStatusCode(HttpStatusCode httpStatusCode)
27+
{
28+
var responseBodyString = "Hello world";
29+
var rpcHttp = await CreateRpcHttpAsync(responseBodyString, httpStatusCode);
30+
rpcHttp.EnableContentNegotiation = true;
31+
32+
dynamic result = GrpcMessageExtensionUtilities.ConvertFromHttpMessageToExpando(rpcHttp);
33+
34+
AssertValidHttpResponseObject(result, "GET", httpStatusCode, responseBodyString, enableContentNegotiation: true);
35+
36+
var headers = result.headers as IDictionary<string, object>;
37+
Assert.Empty(headers);
38+
39+
var cookies = (IEnumerable<object>)result.cookies;
40+
Assert.Empty(cookies);
41+
}
42+
43+
[Fact]
44+
public async Task ConvertFromHttpMessageToExpando_ShouldReturnExpandoObject_WithPOCOResponse()
45+
{
46+
var bookPoco = new Book("Book1", "Author1", 49.99, new DateTime(2012, 1, 1), true, ["C#", "CLR"]);
47+
var responseHeaders = new MapField<string, string>
48+
{
49+
{ "header1", "value1" },
50+
{ "header2", "value2" }
51+
};
52+
var responseCookies = new RepeatedField<RpcHttpCookie>
53+
{
54+
new RpcHttpCookie
55+
{
56+
Name = "cookie1",
57+
Value = "value1",
58+
Domain = new NullableString { Value = "microsoft.com" }
59+
}
60+
};
61+
var rpcHttp = await CreateRpcHttpAsync(bookPoco, headers: responseHeaders, cookies: responseCookies);
62+
63+
dynamic result = GrpcMessageExtensionUtilities.ConvertFromHttpMessageToExpando(rpcHttp);
64+
65+
AssertValidHttpResponseObject(result, "GET", HttpStatusCode.OK);
66+
67+
var headers = result.headers as IDictionary<string, object>;
68+
Assert.Equal(2, headers.Count);
69+
Assert.Equal("value1", headers["header1"]);
70+
Assert.Equal("value2", headers["header2"]);
71+
72+
var cookies = (IEnumerable<object>)result.cookies;
73+
Assert.Single(cookies);
74+
75+
var body = result.body;
76+
Assert.NotNull(body);
77+
78+
if (body is byte[] bodyAsByteArray)
79+
{
80+
var actualBook = JsonSerializer.Deserialize<Book>(Encoding.UTF8.GetString(bodyAsByteArray));
81+
82+
Assert.Equal(bookPoco.Title, actualBook.Title);
83+
Assert.Equal(bookPoco.Author, actualBook.Author);
84+
Assert.Equal(bookPoco.Price, actualBook.Price);
85+
Assert.Equal(bookPoco.PublishedDate.ToString("yyyy-MM-dd"), actualBook.PublishedDate.ToString("yyyy-MM-dd"));
86+
Assert.Equal(bookPoco.IsBestSeller, actualBook.IsBestSeller);
87+
Assert.Equal(bookPoco.Tags, actualBook.Tags);
88+
}
89+
}
90+
91+
[Fact]
92+
public void ConvertFromHttpMessageToExpando_ShouldReturnNull_WhenInputIsNull()
93+
{
94+
var result = GrpcMessageExtensionUtilities.ConvertFromHttpMessageToExpando(null);
95+
96+
Assert.Null(result);
97+
}
98+
99+
private static async Task<RpcHttp> CreateRpcHttpAsync<T>(T body,
100+
HttpStatusCode httpStatusCode = HttpStatusCode.OK, string method = "GET",
101+
MapField<string, string> headers = null, RepeatedField<RpcHttpCookie> cookies = null)
102+
{
103+
var response = new RpcHttp
104+
{
105+
Method = method,
106+
Query = { },
107+
StatusCode = ((int)httpStatusCode).ToString(),
108+
Headers = { },
109+
Cookies = { },
110+
Body = await CreateResponseBodyTypedDataAsync(body)
111+
};
112+
113+
if (headers != null)
114+
{
115+
foreach (var header in headers)
116+
{
117+
response.Headers.Add(header.Key, header.Value);
118+
}
119+
}
120+
121+
if (cookies != null)
122+
{
123+
foreach (var cookie in cookies)
124+
{
125+
response.Cookies.Add(cookie);
126+
}
127+
}
128+
129+
return response;
130+
}
131+
132+
private static async Task<TypedData> CreateResponseBodyTypedDataAsync<T>(T value)
133+
{
134+
TypedData typedData = new();
135+
136+
using var memoryStream = new MemoryStream();
137+
if (value is string stringValue)
138+
{
139+
await memoryStream.WriteAsync(Encoding.UTF8.GetBytes(stringValue));
140+
}
141+
else
142+
{
143+
await JsonSerializer.SerializeAsync(memoryStream, value, typeof(T));
144+
}
145+
146+
memoryStream.Position = 0;
147+
typedData.Bytes = await ByteString.FromStreamAsync(memoryStream);
148+
149+
return typedData;
150+
}
151+
152+
private static void AssertValidHttpResponseObject(dynamic expando, string expectedMethod, HttpStatusCode expectedStatusCode, string expectedResponseBodyContent = null, bool enableContentNegotiation = false)
153+
{
154+
Assert.NotNull(expando);
155+
Assert.Equal(expectedMethod, expando.method);
156+
Assert.Equal(((int)expectedStatusCode).ToString(), expando.statusCode);
157+
Assert.Equal(enableContentNegotiation, expando.enableContentNegotiation);
158+
159+
if (expectedResponseBodyContent is null || expando.body is not byte[] bodyAsByteArray)
160+
{
161+
return;
162+
}
163+
164+
var bodyAsString = Encoding.UTF8.GetString(bodyAsByteArray);
165+
Assert.Equal(expectedResponseBodyContent, bodyAsString);
166+
}
167+
}
168+
169+
internal record Book(string Title, string Author, double Price, DateTime PublishedDate, bool IsBestSeller, List<string> Tags);
170+
}

0 commit comments

Comments
 (0)