HeaderUtils { public static Integer extractIntegerHeader(HttpResponse response, ResponseHeader responseHeader) { NumberFormat numberFormat = NumberFormat.getInstance(Locale.US); numberFormat.setParseIntegerOnly(true); String headerValue = extractHeader(response, responseHeader); try { Number value = numberFormat.parse(headerValue.trim()); return value.intValue(); } catch (Exception e) { return null; } }  static String extractHeader(Map<String, String> headers, ResponseHeader responseHeader); static String extractHeader(HttpResponse response, ResponseHeader responseHeader); static boolean extractBooleanHeader(HttpResponse response, ResponseHeader responseHeader, boolean defaultValue); static Integer extractIntegerHeader(HttpResponse response, ResponseHeader responseHeader); static int extractIntHeader(HttpResponse response, ResponseHeader responseHeader, int defaultValue);  }
@Test public void extractIntegerHeader_shouldReturnIntegerValue() throws Exception { response.addHeader(AD_TIMEOUT.getKey(), "10"); assertThat(HeaderUtils.extractIntegerHeader(response, AD_TIMEOUT)).isEqualTo(10); response.addHeader(AD_TIMEOUT.getKey(), "0"); assertThat(HeaderUtils.extractIntegerHeader(response, AD_TIMEOUT)).isEqualTo(0); response.addHeader(AD_TIMEOUT.getKey(), "-2"); assertThat(HeaderUtils.extractIntegerHeader(response, AD_TIMEOUT)).isEqualTo(-2); }
@Test public void extractIntegerHeader_withDoubleValue_shouldTruncateValue() throws Exception { response.addHeader(AD_TIMEOUT.getKey(), "3.14"); assertThat(HeaderUtils.extractIntegerHeader(response, AD_TIMEOUT)).isEqualTo(3); response.addHeader(AD_TIMEOUT.getKey(), "-3.14"); assertThat(HeaderUtils.extractIntegerHeader(response, AD_TIMEOUT)).isEqualTo(-3); }
@Test public void extractIntegerHeader_whenNoHeaderPresent_shouldReturnNull() throws Exception { assertThat(HeaderUtils.extractIntegerHeader(response, AD_TIMEOUT)).isNull(); response.addHeader(AD_TIMEOUT.getKey(), null); assertThat(HeaderUtils.extractIntegerHeader(response, AD_TIMEOUT)).isNull(); }
@Test public void extractIntegerHeader_withNonsenseStringValue_shouldReturnNull() throws Exception { response.addHeader(AD_TIMEOUT.getKey(), "llama!!guy"); assertThat(HeaderUtils.extractIntegerHeader(response, AD_TIMEOUT)).isNull(); }