/// <summary>
 ///     Settings for DigitalPostForsendelse
 /// </summary>
 public EnvelopeSettings(Forsendelse forsendelse, DocumentBundle documentBundle, Databehandler databehandler, GuidUtility guidUtility, Klientkonfigurasjon konfigurasjon)
 {
     Forsendelse = forsendelse;
     DocumentBundle = documentBundle;
     Databehandler = databehandler;
     GuidUtility = guidUtility;
     Konfigurasjon = konfigurasjon;
 }
            public void ReturnsBinaryEncoding()
            {
                //Arrange

                //Act
                var documentBundle = new DocumentBundle(null, 0, string.Empty);

                //Assert
                Assert.Equal(documentBundle.TransferEncoding, "binary");
            }
            public void ReturnsCmsContentType()
            {
                //Arrange

                //Act
                var documentBundle = new DocumentBundle(null, 0, string.Empty);

                //Assert
                Assert.Equal(documentBundle.ContentType, "application/cms");
            }
            public void SimpleConstructor()
            {
                //Arrange
                var bundleBytes = new byte[] {0x21, 0x22};
                const int billableBytes = 2;
                const string id = "id";

                //Act
                var documentBundle = new DocumentBundle(bundleBytes, billableBytes, id);

                //Assert
                Assert.Equal(bundleBytes, documentBundle.BundleBytes);
                Assert.Equal(billableBytes, documentBundle.BillableBytes);
            }
        private async Task<string> Send(AbstractEnvelope envelope, DocumentBundle asiceDocumentBundle = null)
        {
            if (ClientConfiguration.LoggForespørselOgRespons && RequestResponseLog.IsDebugEnabled)
            {
                RequestResponseLog.Debug($"Utgående {envelope.GetType().Name}, conversationId '{envelope.EnvelopeSettings.Forsendelse?.KonversasjonsId}', messageId '{envelope.EnvelopeSettings.GuidUtility.MessageId}': {envelope.Xml().OuterXml}");
            }

            var requestUri = RequestUri(envelope);
            var httpContent = CreateHttpContent(envelope, asiceDocumentBundle);

            var responseMessage = await HttpClient.PostAsync(requestUri, httpContent).ConfigureAwait(false);
            var responseContent = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (ClientConfiguration.LoggForespørselOgRespons && RequestResponseLog.IsDebugEnabled)
            {
                RequestResponseLog.Debug($" Innkommende {responseContent}");
            }

            return responseContent;
        }
        public async Task<Kvittering> SendMessage(ForretningsmeldingEnvelope envelope, DocumentBundle asiceDocumentBundle)
        {
            var result = await Send(envelope, asiceDocumentBundle).ConfigureAwait(false);

            return KvitteringFactory.GetKvittering(result);
        }
        private static void AddDocumentBundleToMultipart(DocumentBundle documentBundle, MultipartFormDataContent meldingsinnhold)
        {
            if (documentBundle != null)
            {
                var meldingsdata = new ByteArrayContent(documentBundle.BundleBytes);

                meldingsdata.Headers.ContentType = new MediaTypeHeaderValue(documentBundle.ContentType);
                meldingsdata.Headers.Add("Content-Transfer-Encoding", documentBundle.TransferEncoding);
                meldingsdata.Headers.Add("Content-ID", $"<{documentBundle.ContentId}>");

                meldingsinnhold.Add(meldingsdata);
            }
        }
        private static HttpContent CreateHttpContent(AbstractEnvelope envelope, DocumentBundle asiceDocumentBundle)
        {
            var boundary = Guid.NewGuid().ToString();
            var multipartFormDataContent = new MultipartFormDataContent(boundary);

            var contentType = $"Multipart/Related; boundary=\"{boundary}\"; " + "type=\"application/soap+xml\"; " + $"start=\"<{envelope.ContentId}>\"";

            var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(contentType);
            multipartFormDataContent.Headers.ContentType = mediaTypeHeaderValue;

            AddEnvelopeToMultipart(envelope, multipartFormDataContent);
            AddDocumentBundleToMultipart(asiceDocumentBundle, multipartFormDataContent);

            return multipartFormDataContent;
        }