Exemple #1
0
        public void TestSerializeRequest_VersionMissing_ShouldSerializeRequest()
        {
            string             input   = "POST /modules/testModule/sign?api-version=2018-06-28 HTTP/1.1\r\nHost: localhost:8081\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: 100\r\n\r\n";
            HttpRequestMessage request = new HttpRequestMessage();

            request.RequestUri = new Uri("http://localhost:8081/modules/testModule/sign?api-version=2018-06-28", UriKind.Absolute);
            request.Method     = HttpMethod.Post;
            request.Content    = new ByteArrayContent(Encoding.UTF8.GetBytes("test"));
            request.Content.Headers.TryAddWithoutValidation("content-type", "application/json");
            request.Content.Headers.TryAddWithoutValidation("content-length", "100");

            byte[] httpRequestData = HttpRequestResponseSerializer.SerializeRequest(request);
            string requestString   = Encoding.ASCII.GetString(httpRequestData);

            // assert
            var newLines = new[] { '\r', '\n' };
            var actual   = requestString.Split(newLines, StringSplitOptions.RemoveEmptyEntries);
            var expected = input.Split(newLines, StringSplitOptions.RemoveEmptyEntries);

            actual.Length.Should().Be(expected.Length);
            for (int i = 0; i < actual.Length; ++i)
            {
                actual[i].Should().BeEquivalentTo(expected[i]);
            }
        }
        public void TestDeserializeResponse_InvalidEndOfHeaders_ShouldThrow()
        {
            byte[] expected = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\nContent-length: 5\r\n");
            var    memory   = new MemoryStream(expected, true);
            var    stream   = new HttpBufferedStream(memory);

            TestAssert.ThrowsAsync <IOException>(() => HttpRequestResponseSerializer.DeserializeResponseAsync(stream, default)).Wait();
        }
Exemple #3
0
        public async Task TestDeserializeResponse_InvalidHeaderSeparator_ShouldThrow()
        {
            byte[] expected = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\nContent-length=5\r\n\r\nMessage is longer");
            var    memory   = new MemoryStream(expected, true);
            var    stream   = new HttpBufferedStream(memory);

            await TestAssert.ThrowsAsync <HttpRequestException>(() => HttpRequestResponseSerializer.DeserializeResponseAsync(stream, default));
        }
Exemple #4
0
        public async Task TestDeserializeResponse_InvalidEndOfStatusMessage_ShouldThrow()
        {
            byte[]             expected = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK \r\n");
            var                memory   = new MemoryStream(expected, true);
            HttpBufferedStream stream   = new HttpBufferedStream(memory);

            CancellationToken cancellationToken = default(System.Threading.CancellationToken);
            await TestAssert.ThrowsAsync <IOException>(() => HttpRequestResponseSerializer.DeserializeResponseAsync(stream, cancellationToken));
        }
        public void TestDeserializeResponse_InvalidContentLength_ShouldThrow()
        {
            byte[]             expected = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\nContent-length: 5\r\n\r\nMessage is longer");
            var                memory   = new MemoryStream(expected, true);
            HttpBufferedStream stream   = new HttpBufferedStream(memory);

            System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken);
            TestAssert.ThrowsAsync <HttpRequestException>(() => HttpRequestResponseSerializer.DeserializeResponseAsync(stream, cancellationToken)).Wait();
        }
        public void TestDeserializeResponse_MissingReasonPhrase_ShouldThrow()
        {
            byte[]             expected = Encoding.UTF8.GetBytes("HTTP/1.1 200\r\n");
            var                memory   = new MemoryStream(expected, true);
            HttpBufferedStream stream   = new HttpBufferedStream(memory);

            System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken);
            TestAssert.ThrowsAsync <HttpRequestException>(() => HttpRequestResponseSerializer.DeserializeResponseAsync(stream, cancellationToken)).Wait();
        }
        public void TestDeserializeResponse_InvalidEndOfStream_ShouldThrow()
        {
            byte[]             expected = Encoding.UTF8.GetBytes("invalid");
            var                memory   = new MemoryStream(expected, true);
            HttpBufferedStream stream   = new HttpBufferedStream(memory);

            System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken);
            TestAssert.ThrowsAsync <IOException>(() => HttpRequestResponseSerializer.DeserializeResponseAsync(stream, cancellationToken)).Wait();
        }
        public void TestSerializeRequest_RequestUriIsNull_ShouldThrowArgumentNullException()
        {
            HttpRequestMessage request = new HttpRequestMessage();

            request.Method  = HttpMethod.Post;
            request.Content = new ByteArrayContent(Encoding.UTF8.GetBytes("test"));
            request.Content.Headers.TryAddWithoutValidation("content-type", "application/json");

            TestAssert.Throws <ArgumentNullException>(() => HttpRequestResponseSerializer.SerializeRequest(request));
        }
Exemple #9
0
        public void TestSerializeRequest_ContentIsNull_ShouldSerializeRequest()
        {
            string             expected = "GET /modules/testModule/sign?api-version=2018-06-28 HTTP/1.1\r\nHost: localhost:8081\r\nConnection: close\r\n\r\n";
            HttpRequestMessage request  = new HttpRequestMessage();

            request.RequestUri = new Uri("http://localhost:8081/modules/testModule/sign?api-version=2018-06-28", UriKind.Absolute);
            request.Method     = HttpMethod.Get;

            byte[] httpRequestData = new HttpRequestResponseSerializer().SerializeRequest(request);
            string actual          = Encoding.ASCII.GetString(httpRequestData);

            Assert.AreEqual(expected, actual, true, CultureInfo.InvariantCulture);
        }
        public async Task TestDeserializeResponse_StatusLine_ShouldDeserialize()
        {
            byte[]             expected = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\n\r\n");
            var                memory   = new MemoryStream(expected, true);
            HttpBufferedStream stream   = new HttpBufferedStream(memory);

            System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken);
            HttpResponseMessage response = await HttpRequestResponseSerializer.DeserializeResponseAsync(stream, cancellationToken);

            Assert.AreEqual(response.Version, Version.Parse("1.1"));
            Assert.AreEqual(response.StatusCode, System.Net.HttpStatusCode.OK);
            Assert.AreEqual(response.ReasonPhrase, "OK");
        }
Exemple #11
0
        public void TestSerializeRequest_ContentIsNull_ShouldSerializeRequest()
        {
            string expected = $"GET /modules/testModule/sign?api-version=2018-06-28 HTTP/1.1\r\nHost: localhost:8081{Environment.NewLine}Connection: close{Environment.NewLine}\r\n";
            var    request  = new HttpRequestMessage();

            request.RequestUri = new Uri("http://localhost:8081/modules/testModule/sign?api-version=2018-06-28", UriKind.Absolute);
            request.Method     = HttpMethod.Get;

            byte[] httpRequestData = new HttpRequestResponseSerializer().SerializeRequest(request);
            string actual          = Encoding.ASCII.GetString(httpRequestData);

            Assert.Equal(expected.ToLower(), actual.ToLower());
        }
        public void TestSerializeRequest_ContentLengthMissing_ShouldSerializeRequest()
        {
            string             expected = "POST /modules/testModule/sign?api-version=2018-06-28 HTTP/1.1\r\nHost: localhost:8081\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: 4\r\n\r\n";
            HttpRequestMessage request  = new HttpRequestMessage();

            request.RequestUri = new Uri("http://localhost:8081/modules/testModule/sign?api-version=2018-06-28", UriKind.Absolute);
            request.Method     = HttpMethod.Post;
            request.Content    = new ByteArrayContent(Encoding.UTF8.GetBytes("test"));
            request.Content.Headers.TryAddWithoutValidation("content-type", "application/json");

            byte[] httpRequestData = HttpRequestResponseSerializer.SerializeRequest(request);
            string actual          = Encoding.ASCII.GetString(httpRequestData);

            Assert.AreEqual(expected, actual, true, CultureInfo.InvariantCulture);
        }
Exemple #13
0
        public void TestSerializeRequest_ContentLengthMissing_ShouldSerializeRequest()
        {
            string expected = $"POST /modules/testModule/sign?api-version=2018-06-28 HTTP/1.1\r\nHost: localhost:8081{Environment.NewLine}Connection: close{Environment.NewLine}Content-Type: application/json{Environment.NewLine}Content-Length: 4{Environment.NewLine}\r\n";
            var    request  = new HttpRequestMessage();

            request.RequestUri = new Uri("http://localhost:8081/modules/testModule/sign?api-version=2018-06-28", UriKind.Absolute);
            request.Method     = HttpMethod.Post;
            request.Content    = new ByteArrayContent(Encoding.UTF8.GetBytes("test"));
            request.Content.Headers.TryAddWithoutValidation("content-type", "application/json");

            byte[] httpRequestData = new HttpRequestResponseSerializer().SerializeRequest(request);
            string actual          = Encoding.ASCII.GetString(httpRequestData);

            Assert.Equal(expected.ToLower(), actual.ToLower());
        }
        public async Task TestDeserializeResponse_ValidContent_ShouldDeserialize()
        {
            byte[] expected = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\nContent-length: 4\r\n\r\nTest");
            var    memory   = new MemoryStream(expected, true);
            var    stream   = new HttpBufferedStream(memory);

            var response = await HttpRequestResponseSerializer.DeserializeResponseAsync(stream, default);

            Assert.AreEqual(response.Version, Version.Parse("1.1"));
            Assert.AreEqual(response.StatusCode, System.Net.HttpStatusCode.OK);
            Assert.AreEqual(response.ReasonPhrase, "OK");
#if !NETCOREAPP1_1
            Assert.AreEqual(response.Content.Headers.ContentLength, 4);
#endif
            Assert.AreEqual(await response.Content.ReadAsStringAsync(), "Test");
        }
        public async Task TestDeserializeResponse_InvalidHeader_ShouldDeserialize()
        {
            byte[] expected = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\nTest-header: 4\r\n\r\nTest");
            var    memory   = new MemoryStream(expected, true);
            var    stream   = new HttpBufferedStream(memory);

            HttpResponseMessage response = await HttpRequestResponseSerializer.DeserializeResponseAsync(stream, default).ConfigureAwait(false);

            Assert.AreEqual(Version.Parse("1.1"), response.Version);
            Assert.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("OK", response.ReasonPhrase);
#if !NETCOREAPP1_1
            Assert.AreEqual(4, response.Content.Headers.ContentLength);
#endif
            Assert.AreEqual("Test", await response.Content.ReadAsStringAsync());
        }
        static async Task <string> Signer(string base64String)
        {
            if (string.IsNullOrEmpty(base64String))
            {
                return(null);
            }
            try
            {
                string IOTEDGE_MODULEID           = Environment.GetEnvironmentVariable("IOTEDGE_MODULEID");
                string IOTEDGE_MODULEGENERATIONID = Environment.GetEnvironmentVariable("IOTEDGE_MODULEGENERATIONID");
                string IOTEDGE_WORKLOADURI        = Environment.GetEnvironmentVariable("IOTEDGE_WORKLOADURI").Replace("unix://", "");

                UnixEndPoint unixEndpoint = new UnixEndPoint(IOTEDGE_WORKLOADURI);
                Socket       unixSocket   = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
                unixSocket.Connect(unixEndpoint);


                string             httpRequestURI = $"http://localhost/modules/{IOTEDGE_MODULEID}/genid/{IOTEDGE_MODULEGENERATIONID}/sign?api-version=2018-06-28";
                string             requestContent = "{\"keyId\": \"primary\", \"algo\": \"HMACSHA256\", \"data\": \"" + base64String + "\"}";
                HttpRequestMessage request        = new HttpRequestMessage(HttpMethod.Post, httpRequestURI);
                request.Content = new StringContent(requestContent, Encoding.UTF8, "application/json");

                HttpRequestResponseSerializer serializer = new HttpRequestResponseSerializer();
                byte[] requestBytes = serializer.SerializeRequest(request);

                HttpBufferedStream stream = new HttpBufferedStream(new NetworkStream(unixSocket, true));
                await stream.WriteAsync(requestBytes, 0, requestBytes.Length, CancellationToken.None).ConfigureAwait(false);

                if (request.Content != null)
                {
                    await request.Content.CopyToAsync(stream).ConfigureAwait(false);
                }

                HttpResponseMessage response = await serializer.DeserializeResponse(stream, CancellationToken.None).ConfigureAwait(false);

                string responseString = await response.Content.ReadAsStringAsync();

                var responseObject = Newtonsoft.Json.Linq.JObject.Parse(responseString);

                return(responseObject.Value <string>("digest"));
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(null);
            }
        }
Exemple #17
0
        public void TestSerializeRequest_ShouldSerializeRequest()
        {
            string expected = "POST /modules/testModule/sign?api-version=2018-06-28 HTTP/1.1\r\nConnection: close\r\nHost: localhost:8081\r\nContent-Type: application/json\r\nContent-Length: 100";
            var    request  = new HttpRequestMessage();

            request.Method     = HttpMethod.Post;
            request.RequestUri = new Uri("http://localhost:8081/modules/testModule/sign?api-version=2018-06-28", UriKind.Absolute);
            request.Version    = Version.Parse("1.1");
            request.Headers.ConnectionClose = true;
            request.Content = new ByteArrayContent(Encoding.UTF8.GetBytes("test"));
            request.Content.Headers.TryAddWithoutValidation("content-type", "application/json");
            request.Content.Headers.TryAddWithoutValidation("content-length", "100");

            byte[] httpRequestData = new HttpRequestResponseSerializer().SerializeRequest(request);
            string actual          = Encoding.ASCII.GetString(httpRequestData);

            AssertNormalizedValues(expected, actual);
        }
Exemple #18
0
        public void TestSerializeRequest_ContentIsNull_ShouldSerializeRequest()
        {
            string             input   = "GET /modules/testModule/sign?api-version=2018-06-28 HTTP/1.1\r\nHost: localhost:8081\r\nConnection: close\r\n\r\n";
            HttpRequestMessage request = new HttpRequestMessage();

            request.RequestUri = new Uri("http://localhost:8081/modules/testModule/sign?api-version=2018-06-28", UriKind.Absolute);
            request.Method     = HttpMethod.Get;

            byte[] httpRequestData = HttpRequestResponseSerializer.SerializeRequest(request);
            string requestString   = Encoding.ASCII.GetString(httpRequestData);

            // assert
            var newLines = new[] { '\r', '\n' };
            var actual   = requestString.Split(newLines, StringSplitOptions.RemoveEmptyEntries);
            var expected = input.Split(newLines, StringSplitOptions.RemoveEmptyEntries);

            actual.Length.Should().Be(expected.Length);
            for (int i = 0; i < actual.Length; ++i)
            {
                actual[i].Should().BeEquivalentTo(expected[i]);
            }
        }
        public void TestSerializeRequest_MethodMissing_ShouldSerializeRequest()
        {
            string             expected = @"GET /modules/testModule/sign?api-version=2018-06-28 HTTP/1.1
Host: localhost:8081
Connection: close
Content-Type: application/json
Content-Length: 100

";
            HttpRequestMessage request  = new HttpRequestMessage();

            request.RequestUri = new Uri("http://localhost:8081/modules/testModule/sign?api-version=2018-06-28", UriKind.Absolute);
            request.Version    = Version.Parse("1.1");
            request.Content    = new ByteArrayContent(Encoding.UTF8.GetBytes("test"));
            request.Content.Headers.TryAddWithoutValidation("content-type", "application/json");
            request.Content.Headers.TryAddWithoutValidation("content-length", "100");

            byte[] httpRequestData = new HttpRequestResponseSerializer().SerializeRequest(request);
            string actual          = Encoding.ASCII.GetString(httpRequestData);

            Assert.Equal(expected.ToLower(), actual.ToLower());
        }
 public void TestSerializeRequest_RequestIsNull_ShouldThrowArgumentNullException()
 {
     TestAssert.Throws <ArgumentNullException>(() => HttpRequestResponseSerializer.SerializeRequest(null));
 }