public async Task SendJsonBodyMessage()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false))
            {
                var client     = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);
                var sender     = client.CreateSender(scope.QueueName);
                var serializer = new JsonObjectSerializer();
                var testBody   = new TestBody
                {
                    A = "text",
                    B = 5,
                    C = false
                };
                var body = serializer.Serialize(testBody);
                var msg  = new ServiceBusMessage(body);

                await sender.SendMessageAsync(msg);

                var receiver = client.CreateReceiver(scope.QueueName);
                var received = await receiver.ReceiveMessageAsync();

                var receivedBody = received.Body.ToObject <TestBody>(serializer);
                Assert.AreEqual(testBody.A, receivedBody.A);
                Assert.AreEqual(testBody.B, receivedBody.B);
                Assert.AreEqual(testBody.C, receivedBody.C);
            }
        }
예제 #2
0
        public async Task Response_Read_Json_Failure_Transformed()
        {
            TestBody expectedBody = new TestBody
            {
                StringProperty = "This is a failure response",
                IntProperty    = 456
            };

            TestBody responseBody = new TestBody
            {
                StringProperty = "This is a test",
                IntProperty    = 123
            };

            using (HttpClient client = JsonTestClients.RespondWith(HttpStatusCode.BadRequest, responseBody))
            {
                TestBody actualBody = await client
                                      .GetAsync(DefaultRequest)
                                      .ReadContentAsAsync(new JsonFormatter(),
                                                          onFailureResponse: () => new TestBody
                {
                    StringProperty = expectedBody.StringProperty,
                    IntProperty    = expectedBody.IntProperty
                }
                                                          );

                Assert.NotNull(actualBody);
                Assert.NotSame(expectedBody, actualBody);
                Assert.Equal(expectedBody.StringProperty, actualBody.StringProperty);
                Assert.Equal(expectedBody.IntProperty, actualBody.IntProperty);
            }
        }
예제 #3
0
        public async Task WriteAsync_WritesBody()
        {
            var      context     = new DefaultHttpContext();
            var      apiBuilder  = new ApiBuilder(Substitute.For <ISimulation>());
            var      settings    = new ApiSimulatorSettings(apiBuilder);
            TestBody body        = Substitute.ForPartsOf <TestBody>();
            var      apiResponse = new ApiResponse(200, body: body);

            await apiResponse.WriteAsync(context, settings);

            body.Received(1).Written(context, settings);
        }
        public async Task GetSecuredAttestationTokenWithPrivateKey()
        {
            X509Certificate2 fullCertificate = TestEnvironment.PolicyManagementCertificate;

            fullCertificate.PrivateKey = TestEnvironment.PolicyManagementKey;

            // The body of attestation token MUST be a JSON object.
            TestBody body = new TestBody {
                StringField = "Foo",
            };

            var    token           = new AttestationToken(BinaryData.FromObjectAsJson(body), new AttestationTokenSigningKey(fullCertificate));
            string serializedToken = token.Serialize();

            await ValidateSerializedToken(serializedToken, body);
        }
예제 #5
0
        public void Can_Template_With_Model_Und_Culture_Texts()
        {
            var template = new TestBody
            {
                Culture = CultureInfo.GetCultureInfo("de-DE"),
                Model   = new PersonModel
                {
                    FirstName = "John",
                    LastName  = "Doe"
                },
                Texts = new PropertyBag <IDictionary <string, string> >
                {
                    {
                        CultureInfo.GetCultureInfo("de-DE").Name, new Dictionary <string, string>()
                        {
                            { "welcome", "Wilkommen" }
                        }
                    },
                    {
                        CultureInfo.GetCultureInfo("nl-NL").Name, new Dictionary <string, string>()
                        {
                            { "welcome", "Welkom" }
                        }
                    }
                }
            };

            template.Model.ToExpando().DynamicPropery = "DYNAMIC";

            var html = template.ToString();

            Trace.WriteLine(html);

            Assert.That(html, Is.Not.Null);
            Assert.That(html, Is.Not.Empty);
            Assert.That(html.Contains("BODY"));
            Assert.That(html.Contains("LAYOUT"));
            Assert.That(html.Contains("HEADER"));
            Assert.That(html.Contains("FOOTER"));
            Assert.That(html.Contains(template.Model.FirstName));
            Assert.That(html.Contains(template.Model.LastName));
            Assert.That(html.Contains(template.Model.ToExpando().DynamicPropery));
            Assert.That(html.Contains("10.10.2000")); // de-DE
            Assert.That(html.Contains("Wilkommen"));  // de-DE
            Assert.That(template.OutProperties.ContainsKey("subject"));
            Trace.WriteLine("outproperty[key] = " + template.OutProperties["subject"]);
        }
        public void Serialize_HasObject_Serialized()
        {
            // arrange
            var objToSerialize = new TestBody {
                Field = 1
            };

            // act
            var serializer = new JsonMessageSerializer <TestBody>();
            var result     = serializer.Serialize(objToSerialize);

            // assert
            var expectedJson = JsonSerializer.Serialize(objToSerialize);

            Assert.That(result, Is.TypeOf(typeof(TextMessage)));
            Assert.That(((TextMessage)result).Body, Is.EqualTo(expectedJson));
        }
        public void Deserialize_TextMessageHasBody_Deserialize()
        {
            // arrange
            var expectedBody = new TestBody {
                Field = 1
            };
            var message = new TextMessage {
                Body = JsonSerializer.Serialize(expectedBody)
            };

            // act
            var serializer = new JsonMessageSerializer <TestBody>();
            var result     = serializer.Deserialize(message);

            // assert
            Assert.That(result, Is.TypeOf(typeof(TestBody)));
            Assert.That(result.Field, Is.EqualTo(1));
        }
예제 #8
0
        public async Task Response_Read_Json_TreatAsSuccess()
        {
            TestBody expectedBody = new TestBody
            {
                StringProperty = "This is a test",
                IntProperty    = 123
            };

            using (HttpClient client = JsonTestClients.RespondWith(HttpStatusCode.BadRequest, expectedBody))
            {
                TestBody actualBody = await client
                                      .GetAsync(DefaultRequest)
                                      .ReadContentAsAsync <TestBody>(new JsonFormatter(), HttpStatusCode.OK, HttpStatusCode.BadRequest);

                Assert.NotNull(actualBody);
                Assert.NotSame(expectedBody, actualBody);
                Assert.Equal(expectedBody.StringProperty, actualBody.StringProperty);
                Assert.Equal(expectedBody.IntProperty, actualBody.IntProperty);
            }
        }