Headers

Namespace: Rebus
We found 10 examples in language CSharp for this search. You will see 48 fragments of code.

Other methods


        [Fact]
        public void ToResponseHeaders_ReturnsNoHeaders_ByDefault()
        {
            CorsResult result = new CorsResult();

            IDictionary<string, string> headers = result.ToResponseHeaders();

            Assert.Empty(headers);
        }

        [Fact]
        public void ToResponseHeaders_AllowOrigin_AllowOriginHeaderAdded()
        {
            CorsResult result = new CorsResult
            {
                AllowedOrigin = "http://example.com"
            };

            IDictionary<string, string> headers = result.ToResponseHeaders();

            Assert.Equal("http://example.com", headers["Access-Control-Allow-Origin"]);
        }

        [Fact]
        public void ToResponseHeaders_NoAllowOrigin_AllowOriginHeaderNotAdded()
        {
            CorsResult result = new CorsResult
            {
                AllowedOrigin = null
            };

            IDictionary<string, string> headers = result.ToResponseHeaders();

            Assert.DoesNotContain("Access-Control-Allow-Origin", headers.Keys);
        }

        [Fact]
        public void ToResponseHeaders_AllowCredentials_AllowCredentialsHeaderAdded()
        {
            CorsResult result = new CorsResult
            {
                SupportsCredentials = true
            };

            IDictionary<string, string> headers = result.ToResponseHeaders();

            Assert.Equal("true", headers["Access-Control-Allow-Credentials"]);
        }

        [Fact]
        public void ToResponseHeaders_NoAllowCredentials_AllowCredentialsHeaderNotAdded()
        {
            CorsResult result = new CorsResult
            {
                SupportsCredentials = false
            };

            IDictionary<string, string> headers = result.ToResponseHeaders();

            Assert.DoesNotContain("Access-Control-Allow-Credentials", headers.Keys);
        }

        [Fact]
        public void ToResponseHeaders_NoAllowMethods_AllowMethodsHeaderNotAdded()
        {
            CorsResult result = new CorsResult
            {
                // AllowMethods is empty by default
            };

            IDictionary<string, string> headers = result.ToResponseHeaders();

            Assert.DoesNotContain("Access-Control-Allow-Methods", headers.Keys);
        }


		[Test]
		public void Headers_Request ()
		{
			HttpResponseMessage message = new HttpResponseMessage ();
			HttpResponseHeaders headers = message.Headers;

			headers.Add ("accept", "audio");
			Assert.AreEqual ("audio", headers.GetValues ("Accept").First (), "#1");

			headers.Clear ();
			Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#2a");
			Assert.AreEqual ("audio", headers.GetValues ("Accept").First (), "#2");
		}

		[Test]
		public void Headers_Location ()
		{
			HttpResponseMessage message = new HttpResponseMessage ();
			HttpResponseHeaders headers = message.Headers;
			headers.TryAddWithoutValidation ("location", "http://w3.org");
			Assert.AreEqual (new Uri ("http://w3.org"), headers.Location);
		}

		[Test]
		public void Headers_TransferEncoding ()
		{
			HttpResponseMessage message = new HttpResponseMessage ();
			HttpResponseHeaders headers = message.Headers;
			headers.TryAddWithoutValidation ("Transfer-Encoding", "mmm");
			headers.TryAddWithoutValidation ("Transfer-Encoding", "▀");
			headers.TryAddWithoutValidation ("Transfer-Encoding", "zz");

			var a = headers.TransferEncoding;
			Assert.AreEqual (2, a.Count, "#1");

			// Assert.AreEqual ("mmm, zz, ▀", a.ToString (), "#2");
		}


		[Test]
		public void Ctor_Default ()
		{
			var m = new HttpRequestMessage ();
			Assert.IsNull (m.Content, "#1");
			Assert.IsNotNull (m.Headers, "#2");
			Assert.AreEqual (HttpMethod.Get, m.Method, "#3");
			Assert.IsNotNull (m.Properties, "#4");
			Assert.IsNull (m.RequestUri, "#5");
			Assert.AreEqual (new Version (1, 1), m.Version, "#6");

			Assert.AreEqual ("Method: GET, RequestUri: '<null>', Version: 1.1, Content: <null>, Headers:\r\n{\r\n}", m.ToString (), "#7");
		}

		[Test]
		public void Headers_UserAgentExtraWithTabs ()
		{
			HttpRequestMessage message = new HttpRequestMessage ();
			var headers = message.Headers;

			headers.Add ("User-Agent", "A \t  \t B");

			var se = headers.UserAgent.SequenceEqual (
				new[] {
						new ProductInfoHeaderValue ("A", null),
						new ProductInfoHeaderValue ("B", null)
				});

			Assert.IsTrue (se, "#1");
			Assert.AreEqual ("A B", headers.UserAgent.ToString (), "#2");
		}

		[Test]
		public void Headers_UserAgentInvalid ()
		{
			HttpRequestMessage message = new HttpRequestMessage ();
			var headers = message.Headers;

			try {
				headers.Add ("User-Agent", "A(B)");
				Assert.Fail ("#1");
			} catch (FormatException) {
			}
		}

		[Test]
		public void Headers_From_Invalid ()
		{
			HttpRequestMessage message = new HttpRequestMessage ();
			HttpRequestHeaders headers = message.Headers;
			headers.From = null;
			headers.From = "";
			try {
				headers.From = " ";
				Assert.Fail ("#1");
			} catch (FormatException) {
			}
			try {
				headers.From = "test";
				Assert.Fail ("#2");
			} catch (FormatException) {
			}
		}


        [Fact]
        public void WriteCorsHeaders_EmptyCorsResult_EmptyHeaders()
        {
            HttpResponseMessage response = new HttpResponseMessage();
            response.WriteCorsHeaders(new CorsResult());
            Assert.Empty(response.Headers);
        }

        [Test]
        public void SetItem_SetsItem()
        {
            var headers = new Headers
            {
                ["key"] = "value"
            };

            headers.Single().ShouldBe(new KeyValuePair<string, string>("key", "value"));
        }

        [Test]
        public void SetItem_Throws_WhenKeyNull()
        {
            Action action = () => new Headers { [null] = "value" };

            action.ShouldThrow<ArgumentNullException>()
                .ParamName.ShouldBe("name");
        }

        [Test]
        public void SetItem_Throws_WhenKeyEmpty()
        {
            ArgumentException exception = null;
            Action action = () => new Headers { [""] = "a" };

            action.ShouldSatisfyAllConditions(
                () => exception = action.ShouldThrow<ArgumentException>(),
                () => exception.Message.ShouldStartWith(SR.Format(SR.ApiHeaderNameIsEmpty)),
                () => exception.ParamName.ShouldBe("name")
            );
        }

        [Test]
        public void SetItem_SetsMultipleItems_WhenSameKey()
        {
            var headers = new Headers
            {
                ["key"] = "value",
                ["key"] = "two"
            };

            headers.ToArray().ShouldBe(new[] {
                new KeyValuePair<string, string>("key", "value"),
                new KeyValuePair<string, string>("key", "two")
            });
        }

        [Test]
        public void SetItem_ClearsMultipleItems_WhenSameKeyAndValueNull()
        {
            var headers = new Headers
            {
                ["one"] = "me",
                ["key"] = "value",
                ["key"] = "two",
                ["key"] = null
            };

            headers.ToArray().ShouldBe(new[] {
                new KeyValuePair<string, string>("one", "me")
            });
        }

        [TestCase("room")]
        [TestCase("tWO")]
        public void GetItem_GetsPreviouslySetItem(string value)
        {
            var headers = new Headers
            {
                ["loc"] = value
            };

            headers["loc"].ShouldBe(value);
        }


		[Test] // it is somehow allowed ...
		public void AddDuplicate ()
		{
			MessageHeaders headers = new MessageHeaders (MessageVersion.Default);
			headers.Add (MessageHeader.CreateHeader ("Action", wsa1, "urn:foo"));
			headers.Add (MessageHeader.CreateHeader ("Action", wsa1, "urn:bar"));
		}

		[Test]
		public void AddDuplicate2 ()
		{
			MessageHeaders headers = new MessageHeaders (MessageVersion.Default);
			headers.Add (MessageHeader.CreateHeader ("Action", wsa1, "urn:foo", true, "whoa"));
			headers.Add (MessageHeader.CreateHeader ("Action", wsa1, "urn:bar", true, "whee"));
		}

		[Test]
		public void TestConstructor ()
		{
			MessageHeaders headers = new MessageHeaders (MessageVersion.Soap12WSAddressing10);
			Assert.AreEqual (0, headers.Count);

			headers = new MessageHeaders (MessageVersion.Default);
			Assert.AreEqual (0, headers.Count);
		}

		[Test]
		public void TestFindHeaderWithMultipleIdenticalHeaders ()
		{
			Message m = Message.CreateMessage (MessageVersion.Default, "test", 1);
			m.Headers.Add (MessageHeader.CreateHeader (
				"Action", "http://schemas.xmlsoap.org/ws/2004/08/addressing", 1));

			MessageHeaders headers = m.Headers;

			headers.FindHeader ("Action", "http://schemas.xmlsoap.org/ws/2004/08/addressing");
		}

		[Test]
		public void TestGetHeader ()
		{
			Message m = Message.CreateMessage (MessageVersion.Default, "test", 1);
			MessageHeaders headers = m.Headers;

			Assert.AreEqual ("test", headers.GetHeader<string> (0));
		}

		[Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void TestGetHeaderOutOfRange ()
		{
			Message m = Message.CreateMessage (MessageVersion.Default, "test", 1);
			MessageHeaders headers = m.Headers;

			Assert.AreEqual ("test", headers.GetHeader<int> (1));
		}



        [Fact]
        public void HeaderParserNullBuffer()
        {
            HttpHeaders headers;
            InternetMessageFormatHeaderParser parser = InternetMessageFormatHeaderParserTests.CreateHeaderParser(128, out headers);
            Assert.NotNull(parser);
            int bytesConsumed = 0;
            Assert.ThrowsArgumentNull(() => { parser.ParseBuffer(null, 0, ref bytesConsumed); }, "buffer");
        }

        private static IEnumerable<HttpHeaders> CreateHttpHeaders()
        {
            return new HttpHeaders[]
            {
                new HttpRequestMessage().Headers,
                new HttpResponseMessage().Headers,
                new StringContent(String.Empty).Headers,
            };
        }

        private static InternetMessageFormatHeaderParser CreateHeaderParser(int maximumHeaderLength, out HttpHeaders headers)
        {
            headers = new HttpRequestMessage().Headers;
            return new InternetMessageFormatHeaderParser(headers, maximumHeaderLength);
        }

        internal static byte[] CreateBuffer(params string[] headers)
        {
            const string CRLF = "\r\n";
            StringBuilder header = new StringBuilder();
            foreach (var h in headers)
            {
                header.Append(h + CRLF);
            }

            header.Append(CRLF);
            return Encoding.UTF8.GetBytes(header.ToString());
        }


        [Test]
        public void AddLocation_Throws_WhenLocationIsNull()
        {
            Action action = () => new Headers().AddLocation(null);

            action.ShouldThrow<ArgumentNullException>()
                .ParamName.ShouldBe("location");
        }

        [Test]
        public void AddLocation_ReturnsTheSameHeadersInstance()
        {
            var headers = new Headers();

            Headers result = headers.AddLocation(new Uri("https://location.com"));

            result.ShouldBeSameAs(headers);
        }

        [Test]
        public void AddLocation_AddsValidLocationHeader()
        {
            var headers = new Headers();
            var uri = new Uri("https://location.com");

            headers.AddLocation(uri);

            headers["Location"].ShouldBe(uri.AbsoluteUri);
        }

        [TestCase(null)]
        [TestCase("")]
        public void AddWwwAuthenticate_Throws_WhenChallengeIsEmpty(string challenge)
        {
            Action action = () => new Headers().AddWwwAuthenticate(challenge);

            ArgumentException exception = action.ShouldThrow<ArgumentException>();
            exception.ParamName.ShouldBe("challenge");
            exception.Message.ShouldStartWith(SR.Format(SR.ApiHeaderChallengeEmpty));
        }

        [TestCase("simple")]
        [TestCase("bearer")]
        public void AddWwwAuthenticate_AddsValidHeader(string challenge)
        {
            var headers = new Headers();

            headers.AddWwwAuthenticate(challenge);

            headers["WWW-Authenticate"].ShouldBe(challenge);
        }

        [Test]
        public void AddWwwAuthenticate_ReturnsTheSameHeadersInstance()
        {
            var headers = new Headers();

            Headers result = headers.AddWwwAuthenticate("a");

            result.ShouldBeSameAs(headers);
        }


        [Fact]
        public void ToResponseHeaders_ReturnsNoHeaders_ByDefault()
        {
            CorsResult result = new CorsResult();

            IDictionary<string, string> headers = result.ToResponseHeaders();

            Assert.Empty(headers);
        }

        [Fact]
        public void ToResponseHeaders_AllowOrigin_AllowOriginHeaderAdded()
        {
            CorsResult result = new CorsResult
            {
                AllowedOrigin = "http://example.com"
            };

            IDictionary<string, string> headers = result.ToResponseHeaders();

            Assert.Equal("http://example.com", headers["Access-Control-Allow-Origin"]);
        }

        [Fact]
        public void ToResponseHeaders_NoAllowOrigin_AllowOriginHeaderNotAdded()
        {
            CorsResult result = new CorsResult
            {
                AllowedOrigin = null
            };

            IDictionary<string, string> headers = result.ToResponseHeaders();

            Assert.DoesNotContain("Access-Control-Allow-Origin", headers.Keys);
        }

        [Fact]
        public void ToResponseHeaders_AllowCredentials_AllowCredentialsHeaderAdded()
        {
            CorsResult result = new CorsResult
            {
                SupportsCredentials = true
            };

            IDictionary<string, string> headers = result.ToResponseHeaders();

            Assert.Equal("true", headers["Access-Control-Allow-Credentials"]);
        }

        [Fact]
        public void ToResponseHeaders_NoAllowCredentials_AllowCredentialsHeaderNotAdded()
        {
            CorsResult result = new CorsResult
            {
                SupportsCredentials = false
            };

            IDictionary<string, string> headers = result.ToResponseHeaders();

            Assert.DoesNotContain("Access-Control-Allow-Credentials", headers.Keys);
        }

        [Fact]
        public void ToResponseHeaders_NoAllowMethods_AllowMethodsHeaderNotAdded()
        {
            CorsResult result = new CorsResult
            {
                // AllowMethods is empty by default
            };

            IDictionary<string, string> headers = result.ToResponseHeaders();

            Assert.DoesNotContain("Access-Control-Allow-Methods", headers.Keys);
        }


		[SetUp]
		public void Setup ()
		{
			headers = new HttpHeadersMock ();
		}

		[Test]
		public void Add ()
		{
			headers.Add ("aa", "value");
			headers.Add ("aa", "value");
			headers.Add ("Expires", (string) null);
		}

		[Test]
		public void Add_InvalidArguments ()
		{
			try {
				headers.Add (null, "value");
				Assert.Fail ("#1");
			} catch (ArgumentException) {
			}

			try {
				headers.Add ("", "value");
				Assert.Fail ("#2");
			} catch (ArgumentException) {
			}
		}

		[Test]
		public void Clear ()
		{
			headers.Add ("aa", "value");
			headers.Clear ();
		}

		[Test]
		public void GetEnumerator ()
		{
			headers.Add ("aa", "value");
			int i = 0;
			foreach (var entry in headers) {
				++i;
				Assert.AreEqual ("aa", entry.Key);
				var values = entry.Value.ToList ();
				Assert.AreEqual (1, values.Count);
				Assert.AreEqual ("value", values[0]);
			}

			Assert.AreEqual (1, i, "#10");
		}

		[Test]
		public void GetValues ()
		{
			headers.Add ("aa", "v");
			headers.Add ("aa", "v");

			var r = headers.GetValues ("aa").ToList ();
			Assert.AreEqual ("v", r[0], "#1");
			Assert.AreEqual ("v", r[1], "#2");
		}

Rebus.Messages.Headers : Object

Fields :

public static String MessageId
public static String Type
public static String CorrelationId
public static String InReplyTo
public static String CorrelationSequence
public static String ReturnAddress
public static String SenderAddress
public static String RoutingSlipItinerary
public static String RoutingSlipTravelogue
public static String ContentType
public static String ContentEncoding
public static String ErrorDetails
public static String SourceQueue
public static String DeferredUntil
public static String DeferredRecipient
public static String DeferCount
public static String TimeToBeReceived
public static String Express
public static String SentTime
public static String Intent
public static String MessagePayloadAttachmentId

Methods :

public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()