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

Add "cookie" property to rpc http messages #4317

Merged
merged 10 commits into from
Jun 4, 2019
Prev Previous commit
Next Next commit
fixing and adding tests
mhoeger committed May 29, 2019
commit 8e3b796caed76dbc058d5efc3d18ef1d48100762
10 changes: 9 additions & 1 deletion src/WebJobs.Script/Binding/Http/RawScriptResult.cs
Original file line number Diff line number Diff line change
@@ -80,7 +80,15 @@ public async Task ExecuteResultAsync(ActionContext context)
{
mhoeger marked this conversation as resolved.
Show resolved Hide resolved
foreach (var cookie in Cookies)
{
response.Cookies.Append(cookie.Item1, cookie.Item2, cookie.Item3);
// Item3 (CookieOptions) should not be null, but this will behave correctly if it is
if (cookie.Item3 != null)
{
response.Cookies.Append(cookie.Item1, cookie.Item2, cookie.Item3);
}
else
{
response.Cookies.Append(cookie.Item1, cookie.Item2);
}
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
@@ -51,6 +52,37 @@ public async Task HandlesStringContent()
Assert.Equal(200, context.HttpContext.Response.StatusCode);
}

[Fact]
public async Task AddsHttpCookies()
{
var result = new RawScriptResult(null, null)
{
Headers = new Dictionary<string, object>(),
Cookies = new List<Tuple<string, string, CookieOptions>>()
{
new Tuple<string, string, CookieOptions>("firstCookie", "cookieValue", new CookieOptions()
{
SameSite = SameSiteMode.None
}),
new Tuple<string, string, CookieOptions>("secondCookie", "cookieValue2", new CookieOptions()
{
Path = "/",
HttpOnly = true,
MaxAge = TimeSpan.FromSeconds(20)
})
}
};

var context = new ActionContext() { HttpContext = new DefaultHttpContext() };
context.HttpContext.Response.Body = new MemoryStream();
await result.ExecuteResultAsync(context);
context.HttpContext.Response.Headers.TryGetValue("Set-Cookie", out StringValues cookies);

Assert.Equal(2, cookies.Count);
Assert.Equal("firstCookie=cookieValue; path=/", cookies[0]);
Assert.Equal("secondCookie=cookieValue2; max-age=20; path=/; samesite=lax; httponly", cookies[1]);
}

[Fact]
public async Task HandlesStringContent_WithHeader()
{
50 changes: 10 additions & 40 deletions test/WebJobs.Script.Tests/Binding/HttpBindingTests.cs
Original file line number Diff line number Diff line change
@@ -65,62 +65,32 @@ public void ParseResponseObject_ReturnsExpectedResult()
[Fact]
public void ParseResponseObject_WithCookies_ReturnsExpectedResult()
{
var cookieNameValue = new RpcHttpCookie()
var cookieProperties = new Tuple<string, string, CookieOptions>("hello", "world", new CookieOptions()
{
Name = "hello-1",
Value = "world"
};

var cookieWithProperties = new RpcHttpCookie()
{
Name = "hello-2",
Value = "seattle",
Domain = new NullableString()
{
Value = "/"
},
MaxAge = new NullableDouble()
{
Value = 60 * 20
},
HttpOnly = new NullableBool()
{
Value = true
}
};
Domain = "/",
MaxAge = TimeSpan.FromSeconds(60),
HttpOnly = true
});

IList<RpcHttpCookie> rpcCookies = new List<RpcHttpCookie>()
IList<Tuple<string, string, CookieOptions>> cookieContents = new List<Tuple<string, string, CookieOptions>>()
{
cookieNameValue,
cookieWithProperties
cookieProperties
};

dynamic responseObject = new ExpandoObject();
responseObject.Body = "Test Body";
responseObject.Cookies = rpcCookies;
responseObject.Cookies = cookieContents;
responseObject.StatusCode = "202"; // verify string works as well

object content = null;
var statusCode = StatusCodes.Status200OK;
HttpBinding.ParseResponseObject(responseObject, ref content, out IDictionary<string, object> headers, out statusCode, out List<Tuple<string, string, CookieOptions>> cookies, out bool enableContentNegotiationResponse);

Assert.Equal("Test Body", content);
Assert.Same(headers, headers);
Assert.Same(cookieContents, cookies);
mhoeger marked this conversation as resolved.
Show resolved Hide resolved
Assert.Equal(StatusCodes.Status202Accepted, statusCode);
Assert.False(enableContentNegotiationResponse);

Assert.Equal(2, cookies.Count);
var firstCookie = cookies.First();
Assert.Equal("hello-1", firstCookie.Item1);
Assert.Equal("world", firstCookie.Item2);
Assert.Equal(new CookieOptions(), firstCookie.Item3);
var secondCookie = cookies.Last();
Assert.Equal("hello-2", secondCookie.Item1);
Assert.Equal("seattle", firstCookie.Item2);
Assert.Equal("/", firstCookie.Item3.Domain);
Assert.Equal(null, firstCookie.Item3.Expires);
Assert.Equal(true, firstCookie.Item3.HttpOnly);
Assert.Equal(TimeSpan.FromSeconds(60 * 2), firstCookie.Item3.MaxAge);
Assert.Same(headers, headers);
mhoeger marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]