public void APIGatewayProxyResponseTest() { var response = new APIGatewayProxyResponse { StatusCode = 200, Headers = new Dictionary <string, string> { { "Header1", "Value1" }, { "Header2", "Value2" } }, Body = "theBody" }; string serializedJson; using (MemoryStream stream = new MemoryStream()) { var serializer = new JsonSerializer(); serializer.Serialize(response, stream); stream.Position = 0; serializedJson = Encoding.UTF8.GetString(stream.ToArray()); } JObject root = Newtonsoft.Json.JsonConvert.DeserializeObject(serializedJson) as JObject; Assert.Equal(root["statusCode"], 200); Assert.Equal(root["body"], "theBody"); Assert.NotNull(root["headers"]); var headers = root["headers"] as JObject; Assert.Equal(headers["Header1"], "Value1"); Assert.Equal(headers["Header2"], "Value2"); }
protected string SerializeObject <T>(T objectToSerialize) { using (var ms = new MemoryStream()) { _serializer.Serialize(objectToSerialize, ms); return(Encoding.UTF8.GetString(ms.ToArray())); } }
public void APIGatewayAuthorizerResponseTest() { var context = new APIGatewayCustomAuthorizerContextOutput(); context["field1"] = "value1"; context["field2"] = "value2"; var response = new APIGatewayCustomAuthorizerResponse { PrincipalID = "prin1", UsageIdentifierKey = "usageKey", Context = context, PolicyDocument = new APIGatewayCustomAuthorizerPolicy { Version = "2012-10-17", Statement = new List <APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement> { new APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement { Action = new HashSet <string> { "execute-api:Invoke" }, Effect = "Allow", Resource = new HashSet <string> { "*" } } } } }; string serializedJson; using (MemoryStream stream = new MemoryStream()) { var serializer = new JsonSerializer(); serializer.Serialize(response, stream); stream.Position = 0; serializedJson = Encoding.UTF8.GetString(stream.ToArray()); } JObject root = Newtonsoft.Json.JsonConvert.DeserializeObject(serializedJson) as JObject; Assert.Equal("prin1", root["principalId"]); Assert.Equal("usageKey", root["usageIdentifierKey"]); Assert.Equal("value1", root["context"]["field1"]); Assert.Equal("value2", root["context"]["field2"]); Assert.Equal("2012-10-17", root["policyDocument"]["Version"]); Assert.Equal("execute-api:Invoke", root["policyDocument"]["Statement"][0]["Action"][0]); Assert.Equal("Allow", root["policyDocument"]["Statement"][0]["Effect"]); Assert.Equal("*", root["policyDocument"]["Statement"][0]["Resource"][0]); }
public void LexResponse() { using (var fileStream = File.OpenRead("lex-response.json")) { var serializer = new JsonSerializer(); var lexResponse = serializer.Deserialize <LexResponse>(fileStream); Assert.Equal(2, lexResponse.SessionAttributes.Count); Assert.Equal("value1", lexResponse.SessionAttributes["key1"]); Assert.Equal("value2", lexResponse.SessionAttributes["key2"]); Assert.Equal("ElicitIntent, ElicitSlot, ConfirmIntent, Delegate, or Close", lexResponse.DialogAction.Type); Assert.Equal("Fulfilled or Failed", lexResponse.DialogAction.FulfillmentState); Assert.Equal("PlainText or SSML", lexResponse.DialogAction.Message.ContentType); Assert.Equal("message to convey to the user", lexResponse.DialogAction.Message.Content); Assert.Equal("intent-name", lexResponse.DialogAction.IntentName); Assert.Equal(3, lexResponse.DialogAction.Slots.Count); Assert.Equal("value1", lexResponse.DialogAction.Slots["slot-name1"]); Assert.Equal("value2", lexResponse.DialogAction.Slots["slot-name2"]); Assert.Equal("value3", lexResponse.DialogAction.Slots["slot-name3"]); Assert.Equal("slot-name", lexResponse.DialogAction.SlotToElicit); Assert.Equal(3, lexResponse.DialogAction.ResponseCard.Version); Assert.Equal("application/vnd.amazonaws.card.generic", lexResponse.DialogAction.ResponseCard.ContentType); Assert.Equal(1, lexResponse.DialogAction.ResponseCard.GenericAttachments.Count); Assert.Equal("card-title", lexResponse.DialogAction.ResponseCard.GenericAttachments[0].Title); Assert.Equal("card-sub-title", lexResponse.DialogAction.ResponseCard.GenericAttachments[0].SubTitle); Assert.Equal("URL of the image to be shown", lexResponse.DialogAction.ResponseCard.GenericAttachments[0].ImageUrl); Assert.Equal("URL of the attachment to be associated with the card", lexResponse.DialogAction.ResponseCard.GenericAttachments[0].AttachmentLinkUrl); Assert.Equal(1, lexResponse.DialogAction.ResponseCard.GenericAttachments[0].Buttons.Count); Assert.Equal("button-text", lexResponse.DialogAction.ResponseCard.GenericAttachments[0].Buttons[0].Text); Assert.Equal("value sent to server on button click", lexResponse.DialogAction.ResponseCard.GenericAttachments[0].Buttons[0].Value); MemoryStream ms = new MemoryStream(); serializer.Serialize <LexResponse>(lexResponse, ms); ms.Position = 0; var json = new StreamReader(ms).ReadToEnd(); var original = JObject.Parse(File.ReadAllText("lex-response.json")); var serialized = JObject.Parse(json); Assert.True(JToken.DeepEquals(serialized, original), "Serialized object is not the same as the original JSON"); } }
public void KinesisAnalyticsOutputDeliveryResponseTest() { using (var fileStream = File.OpenRead("kinesis-analytics-outputdelivery-response.json")) { var serializer = new JsonSerializer(); var kinesisAnalyticsResponse = serializer.Deserialize <KinesisAnalyticsOutputDeliveryResponse>(fileStream); Assert.Equal(1, kinesisAnalyticsResponse.Records.Count); Assert.Equal("49572672223665514422805246926656954630972486059535892482", kinesisAnalyticsResponse.Records[0].RecordId); Assert.Equal(KinesisAnalyticsOutputDeliveryResponse.OK, kinesisAnalyticsResponse.Records[0].Result); MemoryStream ms = new MemoryStream(); serializer.Serialize <KinesisAnalyticsOutputDeliveryResponse>(kinesisAnalyticsResponse, ms); ms.Position = 0; var json = new StreamReader(ms).ReadToEnd(); var original = JObject.Parse(File.ReadAllText("kinesis-analytics-outputdelivery-response.json")); var serialized = JObject.Parse(json); Assert.True(JToken.DeepEquals(serialized, original), "Serialized object is not the same as the original JSON"); } }
public void KinesisFirehoseResponseTest() { using (var fileStream = File.OpenRead("kinesis-firehose-response.json")) { var serializer = new JsonSerializer(); var kinesisResponse = serializer.Deserialize <KinesisFirehoseResponse>(fileStream); Assert.Equal(1, kinesisResponse.Records.Count); Assert.Equal("49572672223665514422805246926656954630972486059535892482", kinesisResponse.Records[0].RecordId); Assert.Equal(KinesisFirehoseResponse.TRANSFORMED_STATE_OK, kinesisResponse.Records[0].Result); Assert.Equal("SEVMTE8gV09STEQ=", kinesisResponse.Records[0].Base64EncodedData); MemoryStream ms = new MemoryStream(); serializer.Serialize <KinesisFirehoseResponse>(kinesisResponse, ms); ms.Position = 0; var json = new StreamReader(ms).ReadToEnd(); var original = JObject.Parse(File.ReadAllText("kinesis-firehose-response.json")); var serialized = JObject.Parse(json); Assert.True(JToken.DeepEquals(serialized, original), "Serialized object is not the same as the original JSON"); } }