Exemple #1
0
        protected virtual WebApplicationFactory <Startup> CreateFactory()
        {
            var f = new WebApplicationFactory <Startup>()
                    .WithWebHostBuilder(builder => builder.UseContentRoot("."));
            //{
            //    Environment = TestEnvironment
            //};

            var secretRepository = new SecretStore();

            secretRepository.Assign("1234", "ABCD");

            var mrb        = new HmacMessageRepresentationBuilder();
            var calculator = new HmacSignatureCalculator();

            HmacClient = new HmacClient
            {
                ClientId = "1234"
            };
            var hmacHandler = new HmacClientHandler(HmacClient);
            var requestContentMd5Handler = new RequestContentMd5Handler();
            var hmacSigningHandler       = new HmacSigningHandler(secretRepository, mrb, calculator);

            // Inject all the handlers in the correct order
            Client = f.CreateDefaultClient(hmacHandler, requestContentMd5Handler, hmacSigningHandler);

            //Startup = Program.Startup;

            return(f);
        }
        public void Setup()
        {
            client = new HmacClient();

            handler = new HmacClientHandler(client)
            {
                InnerHandler = new StubResponseHandler()
            };
        }
Exemple #3
0
        public async void authorization_header_parameter_is_not_null()
        {
            using (InspectionMessageHandler inspector = new InspectionMessageHandler())
                using (HmacClientHandler hmacHandler = new HmacClientHandler(inspector, appId, secret, new HmacSigningAlgorithm(sb => new HMACSHA256(sb))))
                    using (HttpClient client = new HttpClient(hmacHandler))
                    {
                        await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost/foo"));

                        var req = inspector.LastRequest;

                        Assert.False(string.IsNullOrWhiteSpace(req.Headers.Authorization.Parameter));
                    }
        }
Exemple #4
0
        public async void authorization_header_is_set_with_correct_schema()
        {
            using (InspectionMessageHandler inspector = new InspectionMessageHandler())
                using (HmacClientHandler hmacHandler = new HmacClientHandler(inspector, appId, secret, new HmacSigningAlgorithm(sb => new HMACSHA256(sb))))
                    using (HttpClient client = new HttpClient(hmacHandler))
                    {
                        await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost/foo"));

                        var req = inspector.LastRequest;

                        Assert.Equal(Schemas.HMAC, req.Headers.Authorization.Scheme);
                    }
        }
Exemple #5
0
        public async void custom_headers_are_added_to_request()
        {
            using (InspectionMessageHandler inspector = new InspectionMessageHandler())
                using (HmacClientHandler hmacHandler = new HmacClientHandler(inspector, appId, secret, new HmacSigningAlgorithm(sb => new HMACSHA256(sb))))
                    using (HttpClient client = new HttpClient(hmacHandler))
                    {
                        await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost/foo"));

                        var req = inspector.LastRequest;

                        Assert.Equal(appId, req.Headers.GetValues(Headers.XAppId).First());
                        Assert.False(string.IsNullOrWhiteSpace(req.Headers.GetValues(Headers.XNonce).FirstOrDefault()));
                    }
        }
Exemple #6
0
        private async void butPostScheduleRequestCL_Click(object sender, EventArgs e)
        {
            string              conURL            = ConfigurationManager.AppSettings.Get("PVSWebApiUrl") + "ScheduleRequestCL";
            string              strDebug          = string.Empty;
            HmacClientHandler   hmacClientHandler = new HmacClientHandler();
            HttpClient          client            = HttpClientFactory.Create(hmacClientHandler);
            ScheduleRequest     jsonObject        = PVSLibrary.Common.SampleScheduleRequestCL();
            HttpResponseMessage response          = await client.PostAsJsonAsync(conURL, jsonObject);

            if (response.IsSuccessStatusCode)
            {
                string encContent = await response.Content.ReadAsStringAsync();

                encContent = encContent.Trim('"');
                string strContent = Decrypt(encContent, ConfigurationManager.AppSettings.Get("PVSEncryptKey"), true);
                strDebug = $"Success. Response.Content: { strContent }";
                AdjustTextOutput(strDebug, false);

                var    jsonDef  = new { Name = "" };
                var    jsonObj  = JsonConvert.DeserializeAnonymousType(strContent, jsonDef);
                string jsonName = jsonObj.Name;
                if (jsonName == "ScheduleResponseLC")
                {
                    AdjustTextOutput("Name = " + jsonName.ToString(), false);
                    var jResponseLC = JsonConvert.DeserializeObject <ScheduleResponse>(strContent);
                    DisplayObjectDetailsToOutput(strContent);
                }
                else
                {
                    AdjustTextOutput("Invalid JSON", false);
                }
            }
            else
            {
                strDebug = "Failed to call the API. HTTP Status: " + response.StatusCode + ", Reason: " + response.ReasonPhrase;
                AdjustTextOutput(strDebug, false);
            }
        }