Skip to content

Commit 0cdb159

Browse files
committed
Task 4
1 parent 20e5ba8 commit 0cdb159

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

Libraries/src/Amazon.Lambda.RuntimeSupport/Client/InvocationResponse.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ public class InvocationResponse
3434
/// </summary>
3535
public bool DisposeOutputStream { get; private set; } = true;
3636

37+
/// <summary>
38+
/// Indicates whether this response uses streaming mode.
39+
/// Set internally by the runtime when ResponseStreamFactory.CreateStream() is called.
40+
/// </summary>
41+
internal bool IsStreaming { get; set; }
42+
43+
/// <summary>
44+
/// The ResponseStream instance if streaming mode is used.
45+
/// Set internally by the runtime.
46+
/// </summary>
47+
internal ResponseStream ResponseStream { get; set; }
48+
3749
/// <summary>
3850
/// Construct a InvocationResponse with an output stream that will be disposed by the Lambda Runtime Client.
3951
/// </summary>
@@ -52,6 +64,20 @@ public InvocationResponse(Stream outputStream, bool disposeOutputStream)
5264
{
5365
OutputStream = outputStream ?? throw new ArgumentNullException(nameof(outputStream));
5466
DisposeOutputStream = disposeOutputStream;
67+
IsStreaming = false;
68+
}
69+
70+
/// <summary>
71+
/// Creates an InvocationResponse for a streaming response.
72+
/// Used internally by the runtime.
73+
/// </summary>
74+
internal static InvocationResponse CreateStreamingResponse(ResponseStream responseStream)
75+
{
76+
return new InvocationResponse(Stream.Null, false)
77+
{
78+
IsStreaming = true,
79+
ResponseStream = responseStream
80+
};
5581
}
5682
}
5783
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
using System.IO;
17+
using Xunit;
18+
19+
namespace Amazon.Lambda.RuntimeSupport.UnitTests
20+
{
21+
public class InvocationResponseTests
22+
{
23+
private const long MaxResponseSize = 20 * 1024 * 1024;
24+
25+
/// <summary>
26+
/// Property 17: InvocationResponse Streaming Flag - Existing constructors set IsStreaming to false.
27+
/// Validates: Requirements 7.3, 8.1
28+
/// </summary>
29+
[Fact]
30+
public void Constructor_WithStream_IsStreamingIsFalse()
31+
{
32+
var response = new InvocationResponse(new MemoryStream());
33+
34+
Assert.False(response.IsStreaming);
35+
Assert.Null(response.ResponseStream);
36+
}
37+
38+
[Fact]
39+
public void Constructor_WithStreamAndDispose_IsStreamingIsFalse()
40+
{
41+
var response = new InvocationResponse(new MemoryStream(), false);
42+
43+
Assert.False(response.IsStreaming);
44+
Assert.Null(response.ResponseStream);
45+
}
46+
47+
/// <summary>
48+
/// Property 17: InvocationResponse Streaming Flag - CreateStreamingResponse sets IsStreaming to true.
49+
/// Validates: Requirements 7.3, 8.1
50+
/// </summary>
51+
[Fact]
52+
public void CreateStreamingResponse_SetsIsStreamingTrue()
53+
{
54+
var stream = new ResponseStream(MaxResponseSize);
55+
56+
var response = InvocationResponse.CreateStreamingResponse(stream);
57+
58+
Assert.True(response.IsStreaming);
59+
}
60+
61+
[Fact]
62+
public void CreateStreamingResponse_SetsResponseStream()
63+
{
64+
var stream = new ResponseStream(MaxResponseSize);
65+
66+
var response = InvocationResponse.CreateStreamingResponse(stream);
67+
68+
Assert.Same(stream, response.ResponseStream);
69+
}
70+
71+
[Fact]
72+
public void CreateStreamingResponse_DoesNotDisposeOutputStream()
73+
{
74+
var stream = new ResponseStream(MaxResponseSize);
75+
76+
var response = InvocationResponse.CreateStreamingResponse(stream);
77+
78+
Assert.False(response.DisposeOutputStream);
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)