Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

Commit

Permalink
Merge pull request #186 from difi/FikserUseragentBug
Browse files Browse the repository at this point in the history
Fikser useragent bug
  • Loading branch information
asjafjell authored Oct 4, 2016
2 parents be9664c + bc76bb6 commit d71cf78
Show file tree
Hide file tree
Showing 19 changed files with 152 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ public async Task Returns_transport_error_receipt()
{
//Arrange
var sikkerDigitalPostKlient = DomainUtility.GetSikkerDigitalPostKlientQaOffentlig();
var fakeHttpClientHandlerResponse = new FakeHttpClientHandlerResponse(XmlResource.Response.GetTransportError().OuterXml, HttpStatusCode.BadRequest);
var fakeHttpClientHandlerResponse = new FakeResponseHandler()
{
HttpContent = new StringContent(XmlResource.Response.GetTransportError().OuterXml),
StatusCode = HttpStatusCode.BadRequest
};
sikkerDigitalPostKlient.RequestHelper.HttpClient = new HttpClient(fakeHttpClientHandlerResponse);

//Act
Expand All @@ -86,7 +90,10 @@ public async Task Calls_all_dokumentpakke_prosessors()
var sikkerDigitalPostKlient = new SikkerDigitalPostKlient(DomainUtility.GetDatabehandler(), klientkonfigurasjon);

DomainUtility.GetSikkerDigitalPostKlientQaOffentlig();
var fakeHttpClientHandlerResponse = new FakeHttpClientHandlerResponse(XmlResource.Response.GetTransportOk().OuterXml, HttpStatusCode.OK);
var fakeHttpClientHandlerResponse = new FakeResponseHandler()
{
HttpContent = new StringContent(XmlResource.Response.GetTransportOk().OuterXml),
};
sikkerDigitalPostKlient.RequestHelper.HttpClient = new HttpClient(fakeHttpClientHandlerResponse);

//Act
Expand Down Expand Up @@ -116,7 +123,10 @@ public async Task Throws_exception_on_response_not_matching_request()
{
//Arrange
var sikkerDigitalPostKlient = DomainUtility.GetSikkerDigitalPostKlientQaOffentlig();
var fakeHttpClientHandlerResponse = new FakeHttpClientHandlerResponse(XmlResource.Response.GetTransportOk().OuterXml, HttpStatusCode.OK);
var fakeHttpClientHandlerResponse = new FakeResponseHandler()
{
HttpContent = new StringContent(XmlResource.Response.GetTransportOk().OuterXml),
};
sikkerDigitalPostKlient.RequestHelper.HttpClient = new HttpClient(fakeHttpClientHandlerResponse);

//Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@
<Compile Include="Entiteter\Post\ForsendelseTests.cs" />
<Compile Include="Extensions\EnumExtensionsTests.cs" />
<Compile Include="Exceptions\XmlValidationExceptionTests.cs" />
<Compile Include="Fakes\FakeHttpClientHandler.cs" />
<Compile Include="Fakes\FakeHttpClientHandlerResponse.cs" />
<Compile Include="Fakes\FakeResponseHandler.cs" />
<Compile Include="Internal\AsicE\AsiceAttachableProcessorTests.cs" />
<Compile Include="Internal\AsicE\AsiceGeneratorTests.cs" />
<Compile Include="Internal\AsicE\DocumentBundleTests.cs" />
Expand Down

This file was deleted.

This file was deleted.

30 changes: 30 additions & 0 deletions Difi.SikkerDigitalPost.Klient.Tester/Fakes/FakeResponseHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Difi.SikkerDigitalPost.Klient.Tester.Fakes
{
internal class FakeResponseHandler : DelegatingHandler
{
public Action<HttpRequestMessage> TestingAction { get; set; }

public HttpStatusCode StatusCode { get; set; } = HttpStatusCode.OK;

public HttpContent HttpContent { get; set; } = new StringContent("Sucess");

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage requestMessage, CancellationToken cancellationToken)
{
TestingAction?.Invoke(requestMessage);

var response = new HttpResponseMessage
{
Content = HttpContent,
StatusCode = StatusCode
};

return await Task.FromResult(response).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Net;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml;
using Difi.SikkerDigitalPost.Klient.Domene.Entiteter.Kvitteringer.Transport;
using Difi.SikkerDigitalPost.Klient.Internal;
using Difi.SikkerDigitalPost.Klient.Internal.AsicE;
Expand All @@ -18,7 +20,7 @@ public class RequestHelperTests
public class ConstructorMethod : RequestHelperTests
{
[Fact]
public void InitializesFields()
public void Initializes_fields()
{
//Arrange
var clientConfiguration = new Klientkonfigurasjon(Miljø.FunksjoneltTestmiljø);
Expand All @@ -34,15 +36,19 @@ public void InitializesFields()
public class SendMethod : RequestHelperTests
{
[Fact]
public async Task ReturnsReceiptSuccessfully()
public async Task Returns_receipt_successfully()
{
//Arrange
var forretningsmeldingEnvelope = DomainUtility.GetForretningsmeldingEnvelope();

var documentBundle = AsiceGenerator.Create(DomainUtility.GetForsendelseSimple(), new GuidUtility(), DomainUtility.GetAvsenderCertificate(), DomainUtility.GetKlientkonfigurasjon());

var requestHelper = new RequestHelper(new Klientkonfigurasjon(Miljø.FunksjoneltTestmiljø));
var fakeHttpClientHandlerResponse = new FakeHttpClientHandlerResponse(XmlResource.Response.GetTransportOk().OuterXml, HttpStatusCode.OK);
var fakeHttpClientHandlerResponse = new FakeResponseHandler()
{
HttpContent = new StringContent(XmlResource.Response.GetTransportOk().OuterXml),
StatusCode = HttpStatusCode.OK
};
requestHelper.HttpClient = new HttpClient(fakeHttpClientHandlerResponse);

//Act
Expand All @@ -51,5 +57,33 @@ public async Task ReturnsReceiptSuccessfully()
Assert.IsType<TransportOkKvittering>(kvittering);
}
}

public class HttpClientProperty
{
[Fact]
public async Task Adds_user_agent()
{
//Arrange
var forretningsmeldingEnvelope = DomainUtility.GetForretningsmeldingEnvelope();
var documentBundle = AsiceGenerator.Create(DomainUtility.GetForsendelseSimple(), new GuidUtility(), DomainUtility.GetAvsenderCertificate(), DomainUtility.GetKlientkonfigurasjon());

Action<HttpRequestMessage> testingAction = message =>
{
Assert.Contains("sikker-digital-post", message.Headers.UserAgent.ToString());
};

var requestHelper = new RequestHelper(
new Klientkonfigurasjon(Miljø.FunksjoneltTestmiljø),
new FakeResponseHandler()
{
TestingAction = testingAction,
HttpContent = new StringContent(XmlResource.Response.GetTransportOk().OuterXml)
}
);

//Act
await requestHelper.SendMessage(forretningsmeldingEnvelope, documentBundle).ConfigureAwait(false);
}
}
}
}
3 changes: 2 additions & 1 deletion Difi.SikkerDigitalPost.Klient.Tester/SmokeTestsHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading;
using Difi.SikkerDigitalPost.Klient.Api;
using Difi.SikkerDigitalPost.Klient.Domene.Entiteter;
using Difi.SikkerDigitalPost.Klient.Domene.Entiteter.Aktører;
using Difi.SikkerDigitalPost.Klient.Domene.Entiteter.Kvitteringer;
using Difi.SikkerDigitalPost.Klient.Domene.Entiteter.Kvitteringer.Forretning;
Expand All @@ -14,7 +15,7 @@ namespace Difi.SikkerDigitalPost.Klient.Tester
{
internal class SmokeTestsHelper
{
private const string BringOrganisasjonsnummer = "988015814";
private static readonly Organisasjonsnummer BringOrganisasjonsnummer = new Organisasjonsnummer("988015814");
private const string BringThumbprint = "2d7f30dd05d3b7fc7ae5973a73f849083b2040ed";
private readonly SikkerDigitalPostKlient _klient;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class GetMiljøMethod : MiljøTests
public void Returnerer_initialisert_funksjonelt_testmiljø()
{
//Arrange
var url = "https://qaoffentlig.meldingsformidler.digipost.no/api/ebms";
var url = "https://qaoffentlig.meldingsformidler.digipost.no/api/";
var miljø = Miljø.FunksjoneltTestmiljø;
var sertifikater = CertificateChainUtility.FunksjoneltTestmiljøSertifikater();

Expand All @@ -51,7 +51,7 @@ public void Returnerer_initialisert_funksjonelt_testmiljø()
public void Returnerer_initialisert_produksjonsmiljø()
{
//Arrange
var url = "https://meldingsformidler.digipost.no/api/ebms";
var url = "https://meldingsformidler.digipost.no/api/";
var miljø = Miljø.Produksjonsmiljø;
var sertifikater = CertificateChainUtility.ProduksjonsSertifikater();

Expand Down
4 changes: 3 additions & 1 deletion Difi.SikkerDigitalPost.Klient.Testklient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using ApiClientShared;
using Common.Logging;
using Difi.SikkerDigitalPost.Klient.Api;
using Difi.SikkerDigitalPost.Klient.Domene.Entiteter;
using Difi.SikkerDigitalPost.Klient.Domene.Entiteter.Aktører;
using Difi.SikkerDigitalPost.Klient.Domene.Entiteter.FysiskPost;
using Difi.SikkerDigitalPost.Klient.Domene.Entiteter.Kvitteringer;
Expand Down Expand Up @@ -46,7 +47,8 @@ private static void SendPost()
var postInfo = GenererPostInfo(ErDigitalPostMottaker, ErNorskBrev);
var avsender = new Avsender(Settings.Default.OrgnummerPosten);

var databehandler = new Databehandler(Settings.Default.OrgnummerPosten,
var databehandler = new Databehandler(
new Organisasjonsnummer(Settings.Default.OrgnummerPosten),
Settings.Default.DatabehandlerSertifikatThumbprint);
avsender.Avsenderidentifikator = "digipost";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,19 @@
<HintPath>..\packages\difi-felles-utility-dotnet.0.8.0.27738\lib\net45\Difi.Felles.Utility.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Security" />
<Reference Include="System.Xml.Linq" />
Expand Down Expand Up @@ -103,7 +111,7 @@
<Compile Include="Envelope\Kvitteringsbekreftelse\KvitteringsbekreftelseEnvelope.cs" />
<Compile Include="Envelope\Kvitteringsforespørsel\KvitteringsforespørselEnvelope.cs" />
<Compile Include="Envelope\Abstract\EnvelopeXmlPart.cs" />
<Compile Include="Handlers\UserAgentHttpHandler.cs" />
<Compile Include="Handlers\UserAgentHandler.cs" />
<Compile Include="Internal\RequestHelper.cs" />
<Compile Include="KvitteringFactory.cs" />
<Compile Include="Kvitteringsparser.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private XmlElement AvsenderElement()
{
var organisasjon = avsender.AppendChildElement("organisasjon", "ns9", NavneromUtility.DifiSdpSchema10, Context);
organisasjon.SetAttribute("authority", "iso6523-actorid-upis");
organisasjon.InnerText = Settings.Forsendelse.Avsender.Organisasjonsnummer.Iso6523();
organisasjon.InnerText = Settings.Forsendelse.Avsender.Organisasjonsnummer.WithCountryCode;

var avsenderIdentifikator = Settings.Forsendelse.Avsender.Avsenderidentifikator;
if (avsenderIdentifikator != string.Empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private XmlElement PartyInfoElement()
{
var partyId = from.AppendChildElement("PartyId", "eb", NavneromUtility.EbXmlCore, Context);
partyId.SetAttribute("type", "urn:oasis:names:tc:ebcore:partyid-type:iso6523:9908");
partyId.InnerText = Settings.Databehandler.Organisasjonsnummer.Iso6523();
partyId.InnerText = Settings.Databehandler.Organisasjonsnummer.WithCountryCode;

var role = from.AppendChildElement("Role", "eb", NavneromUtility.EbXmlCore, Context);
role.InnerText = "urn:sdp:avsender";
Expand All @@ -75,7 +75,7 @@ private XmlElement PartyInfoElement()
{
var partyId = to.AppendChildElement("PartyId", "eb", NavneromUtility.EbXmlCore, Context);
partyId.SetAttribute("type", "urn:oasis:names:tc:ebcore:partyid-type:iso6523:9908");
partyId.InnerText = Settings.Konfigurasjon.MeldingsformidlerOrganisasjon.Iso6523();
partyId.InnerText = Settings.Konfigurasjon.MeldingsformidlerOrganisasjon.WithCountryCode;

var role = to.AppendChildElement("Role", "eb", NavneromUtility.EbXmlCore, Context);
role.InnerText = "urn:sdp:meldingsformidler";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,12 @@

namespace Difi.SikkerDigitalPost.Klient.Handlers
{
internal class UserAgentHttpHandler : DelegatingHandler
internal class UserAgentHandler : DelegatingHandler
{
public UserAgentHttpHandler()
: base(new HttpClientHandler())
{
}

public UserAgentHttpHandler(HttpMessageHandler innerHandler)
: base(innerHandler)
{
}

protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Add("User-Agent", GetAssemblyVersion());

return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}

Expand All @@ -34,7 +23,7 @@ private static string GetAssemblyVersion()

var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version;

return $"difi-sikker-digital-post-klient/{assemblyVersion} (.NET/{netVersion})";
return $"difi-sikker-digital-post-klient-dotnet/{assemblyVersion} (.NET/{netVersion})";
}
}
}
2 changes: 1 addition & 1 deletion Difi.SikkerDigitalPost.Klient/Internal/AsicE/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private XmlElement AvsenderNode()
{
var organisasjon = avsender.AppendChildElement("organisasjon", NavneromUtility.DifiSdpSchema10, _manifestXml);
organisasjon.SetAttribute("authority", "iso6523-actorid-upis");
organisasjon.InnerText = Avsender.Organisasjonsnummer.Iso6523();
organisasjon.InnerText = Avsender.Organisasjonsnummer.WithCountryCode;

var avsenderId = Avsender.Avsenderidentifikator;
if (!string.IsNullOrWhiteSpace(avsenderId))
Expand Down
Loading

0 comments on commit d71cf78

Please sign in to comment.