Skip to content

Commit

Permalink
Adds support for "basic" authentication with URL credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
exalted committed Apr 7, 2017
1 parent 51ec47c commit 5ce415c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/Dispatch.Api.Client.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ public async Task Test_Delete()
Assert.True(result);
}

[Fact]
[UsedImplicitly]
public async Task Test_BasicAuth()
{
var schedule = new DateTimeOffset(DateTime.Now);

var request = new ScheduledBundleRequest(schedule);

request.HttpRequests.Add(NewHttpRequestMessageWithBasicAuth());

var response = await this.client.ScheduleAsync(request)
.ConfigureAwait(false);

Assert.NotNull(response.Id);
Assert.Equal(schedule, response.Schedule);
}

[Fact]
[UsedImplicitly]
public async Task Test_MailAttachments()
Expand Down Expand Up @@ -127,6 +144,11 @@ private static HttpRequestMessage NewHttpRequestMessage()
return new HttpRequestMessage("HEAD", "http://requestb.in/188o2xj1");
}

private static HttpRequestMessage NewHttpRequestMessageWithBasicAuth()
{
return new HttpRequestMessage("HEAD", "http://user:pass@requestb.in/1lq1vzu1");
}

private static ApexnetPushNotification NewApexnetPushNotification()
{
const string AuthKey = "3892DB2C-53A9-4B57-9638-08C1E91319C8";
Expand Down
19 changes: 16 additions & 3 deletions src/Messaging/Http/HttpRequestSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ namespace Apexnet.Messaging.Http
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

public class HttpRequestSender : IDisposable
{
Expand Down Expand Up @@ -33,10 +35,21 @@ private HttpRequestSender(HttpClient smtpClient)

public void Send(HttpRequestMessage message)
{
var httpRequestMessage = new System.Net.Http.HttpRequestMessage(
HttpMethodsMapping[message.Method.ToUpperInvariant()],
message.RequestUri);
var requestUri = new Uri(message.RequestUri);

var httpRequestMessage =
new System.Net.Http.HttpRequestMessage(
HttpMethodsMapping[message.Method.ToUpperInvariant()],
requestUri);

if (!string.IsNullOrWhiteSpace(requestUri.UserInfo))
{
httpRequestMessage.Headers.Authorization = new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(Encoding.UTF8.GetBytes(requestUri.UserInfo)));
}

// This will wait Taks to complete, but we won't know if the request "succeeded" or "failed".
this.httpClient.SendAsync(httpRequestMessage)
.Wait();
}
Expand Down

0 comments on commit 5ce415c

Please sign in to comment.