From 165ca12e6d6f7f305417fbce35e466f567821fb1 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 25 Jan 2018 15:50:19 +0100 Subject: [PATCH] Add doesExist() to HeaderAssertions for WebTestClient --- .../web/reactive/server/HeaderAssertions.java | 13 +++++++++++ .../reactive/server/HeaderAssertionTests.java | 23 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/HeaderAssertions.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/HeaderAssertions.java index 08617d179c..19db07df29 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/HeaderAssertions.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/HeaderAssertions.java @@ -32,6 +32,7 @@ import static org.springframework.test.util.AssertionErrors.*; * * @author Rossen Stoyanchev * @author Brian Clozel + * @author Sam Brannen * @since 5.0 * @see WebTestClient.ResponseSpec#expectHeader() */ @@ -72,6 +73,18 @@ public class HeaderAssertions { return this.responseSpec; } + /** + * Expect that the header with the given name is present. + * @since 5.0.3 + */ + public WebTestClient.ResponseSpec doesExist(String name) { + if (!getHeaders().containsKey(name)) { + String message = getMessage(name) + " does not exist"; + this.exchangeResult.assertWithDiagnostics(() -> fail(message)); + } + return this.responseSpec; + } + /** * Expect that the header with the given name is not present. */ diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.java index 3d2fbd8561..01b40b7d50 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,6 +37,7 @@ import static org.mockito.Mockito.*; * Unit tests for {@link HeaderAssertions}. * * @author Rossen Stoyanchev + * @author Sam Brannen * @since 5.0 */ public class HeaderAssertionTests { @@ -124,6 +125,26 @@ public class HeaderAssertionTests { } } + @Test + public void doesExist() { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON_UTF8); + HeaderAssertions assertions = headerAssertions(headers); + + // Success + assertions.doesExist("Content-Type"); + + try { + assertions.doesExist("Framework"); + fail("Header should not exist"); + } + catch (AssertionError error) { + Throwable cause = error.getCause(); + assertNotNull(cause); + assertEquals("Response header 'Framework' does not exist", cause.getMessage()); + } + } + @Test public void doesNotExist() { HttpHeaders headers = new HttpHeaders();