Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use new HttpContent each time for HttpRequestMock #4199

Merged
merged 10 commits into from
Jul 24, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@

namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Testing.HttpRequestMocks
{
/// <summary>
/// Base class for all http request mocks.
/// </summary>
public abstract class HttpRequestMock
{
/// <summary>
/// In the constructor of MockHttpRequestMiddleware, this function will be called to setup a global handler.
/// </summary>
/// <param name="handler">The global handler.</param>
public abstract void Setup(MockHttpMessageHandler handler);

protected void RegisterSourcePath(string path, int line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Testing.HttpRequestMocks
{
/// <summary>
/// Mock http request in sequence order. The last response will be repeated.
/// </summary>
public class HttpRequestSequenceMock : HttpRequestMock
{
[JsonProperty("$kind")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
using System.ComponentModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using static Microsoft.Bot.Builder.Dialogs.Adaptive.Actions.HttpRequest;

namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Testing.HttpRequestMocks
{
/// <summary>
/// Http Response Mock (used in HttpRequestSequenceMock).
/// </summary>
public class HttpResponseMock
{
public enum ContentTypes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Net.Http;

namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Testing.HttpRequestMocks
{
/// <summary>
/// Convert and store the actual content of HttpResonseMock.
/// </summary>
public class HttpResponseMockContent
xieofxie marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly HttpResponseMock.ContentTypes contentType;

private readonly object content;

public HttpResponseMockContent()
{
contentType = HttpResponseMock.ContentTypes.String;
content = string.Empty;
}

public HttpResponseMockContent(HttpResponseMock httpResponseMock)
{
contentType = httpResponseMock.ContentType;
switch (contentType)
{
case HttpResponseMock.ContentTypes.String:
content = httpResponseMock.Content == null ? string.Empty : httpResponseMock.Content.ToString();
break;
case HttpResponseMock.ContentTypes.ByteArray:
content = Convert.FromBase64String(httpResponseMock.Content == null ? string.Empty : httpResponseMock.Content.ToString());
break;
default:
throw new NotSupportedException($"{httpResponseMock.ContentType} is not supported yet!");
}
}

/// <summary>
/// Return a new HttpContent based on content.
/// </summary>
/// <returns>A new HttpContent.</returns>
public HttpContent GetHttpContent()
xieofxie marked this conversation as resolved.
Show resolved Hide resolved
{
switch (contentType)
{
case HttpResponseMock.ContentTypes.String:
return new StringContent((string)content);
case HttpResponseMock.ContentTypes.ByteArray:
return new ByteArrayContent((byte[])content);
default:
throw new NotSupportedException($"{contentType} is not supported yet!");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,37 @@

namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Testing.HttpRequestMocks
{
/// <summary>
/// Manage Sequence Response for HttpRequestSequenceMock.
/// </summary>
public class SequenceResponseManager
{
private int _id;
private List<HttpContent> _contents;
private List<HttpResponseMockContent> _contents = new List<HttpResponseMockContent>();

public SequenceResponseManager(List<HttpResponseMock> responses)
xieofxie marked this conversation as resolved.
Show resolved Hide resolved
{
_id = 0;
if (responses == null || responses.Count == 0)
{
_contents = new List<HttpContent>()
{
new StringContent(string.Empty)
};
// Create an empty content for response.
_contents.Add(new HttpResponseMockContent());
}
else
{
_contents = responses.Select(r =>
foreach (var response in responses)
{
switch (r.ContentType)
{
case HttpResponseMock.ContentTypes.String:
return (HttpContent)new StringContent(r.Content == null ? string.Empty : r.Content.ToString());
case HttpResponseMock.ContentTypes.ByteArray:
var bytes = Convert.FromBase64String(r.Content == null ? string.Empty : r.Content.ToString());
return (HttpContent)new ByteArrayContent(bytes);
default:
throw new NotSupportedException($"{r.ContentType} is not supported yet!");
}
}).ToList();
_contents.Add(new HttpResponseMockContent(response));
}
}
}

/// <summary>
/// Return the content in sequence order. The last one will be repeated.
/// </summary>
/// <returns>
/// The HttpContent.
/// </returns>
public HttpContent GetContent()
{
var result = _contents[_id];
Expand All @@ -49,7 +47,8 @@ public HttpContent GetContent()
_id++;
}

return result;
// We create a new one here in case the consumer will dispose the content object.
return result.GetHttpContent();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"responses": [
{
"content": "fallback response"
},
{
"contentType": "ByteArray",
// Convert.ToBase64String(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(obj)))
"content": "eyJkYXRhIjoicmVwZWF0ZWQgQnl0ZUFycmF5IHJlc3BvbnNlIn0="
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@
{
"$kind": "Microsoft.SendActivity",
"activity": "${dialog.result.content}"
},
{
"$kind": "Microsoft.HttpRequest",
"responseType": "Json",
"method": "POST",
"url": "http://127.0.0.1",
"resultProperty": "dialog.result"
},
{
"$kind": "Microsoft.SendActivity",
"activity": "${dialog.result.content.data}"
},
{
"$kind": "Microsoft.HttpRequest",
"responseType": "Json",
"method": "POST",
"url": "http://127.0.0.1",
"resultProperty": "dialog.result"
},
{
"$kind": "Microsoft.SendActivity",
"activity": "${dialog.result.content.data}"
}
]
}
Expand Down Expand Up @@ -110,6 +132,14 @@
{
"$kind": "Microsoft.Test.AssertReply",
"text": "repeated response"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "repeated ByteArray response"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "repeated ByteArray response"
}
]
}
3 changes: 2 additions & 1 deletion tests/tests.schema
Original file line number Diff line number Diff line change
Expand Up @@ -3732,7 +3732,8 @@
"none",
"json",
"activity",
"activities"
"activities",
"binary"
],
"default": "json"
},
Expand Down