@Test public void test206ResponseGeneratedFromCacheMustHaveContentRangeOrMultipartByteRangesContentType() throws Exception { final ClassicHttpRequest req1 = new BasicClassicHttpRequest("GET", "/"); final ClassicHttpResponse resp1 = HttpTestUtils.make200Response(); resp1.setHeader("ETag", "\"etag\""); resp1.setHeader("Cache-Control", "max-age=3600"); final ClassicHttpRequest req2 = new BasicClassicHttpRequest("GET", "/"); req2.setHeader("Range", "bytes=0-50"); backendExpectsAnyRequestAndReturn(resp1).times(1, 2); replayMocks(); execute(req1); final ClassicHttpResponse result = execute(req2); verifyMocks(); if (HttpStatus.SC_PARTIAL_CONTENT == result.getCode()) { if (result.getFirstHeader("Content-Range") == null) { final HeaderElement elt = MessageSupport.parse(result.getFirstHeader("Content-Type"))[0]; Assert.assertTrue("multipart/byteranges".equalsIgnoreCase(elt.getName())); Assert.assertNotNull(elt.getParameterByName("boundary")); Assert.assertNotNull(elt.getParameterByName("boundary").getValue()); Assert.assertFalse("".equals(elt.getParameterByName("boundary").getValue().trim())); } } }
@Override public NameValuePair getParameterByName(final String name) { Args.notNull(name, "Name"); NameValuePair found = null; for (final NameValuePair current : this.parameters) { if (current.getName().equalsIgnoreCase(name)) { found = current; break; } } return found; }
@Test public void testConstructor3() throws Exception { final HeaderElement element = new BasicHeaderElement("name", "value", new NameValuePair[] { new BasicNameValuePair("param1", "value1"), new BasicNameValuePair("param2", "value2") } ); Assert.assertEquals("name", element.getName()); Assert.assertEquals("value", element.getValue()); Assert.assertEquals(2, element.getParameters().length); Assert.assertEquals("value1", element.getParameterByName("param1").getValue()); Assert.assertEquals("value2", element.getParameterByName("param2").getValue()); }
@Override public NameValuePair getParameterByName(final String name) { Args.notNull(name, "Name"); NameValuePair found = null; for (final NameValuePair current : this.parameters) { if (current.getName().equalsIgnoreCase(name)) { found = current; break; } } return found; }
static String extractCN(final String subjectPrincipal) throws SSLException { if (subjectPrincipal == null) { return null; } final List<NameValuePair> attributes = DistinguishedNameParser.INSTANCE.parse(subjectPrincipal); for (final NameValuePair attribute: attributes) { if (TextUtils.isBlank(attribute.getName()) || attribute.getValue() == null) { throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name"); } if (attribute.getName().equalsIgnoreCase("cn")) { return attribute.getValue(); } } return null; }
@Test public void testParamByName() throws Exception { final String s = "name = value; param1 = value1; param2 = value2"; final CharArrayBuffer buf = new CharArrayBuffer(64); buf.append(s); final ParserCursor cursor = new ParserCursor(0, buf.length()); final HeaderElement element = BasicHeaderValueParser.INSTANCE.parseHeaderElement(buf, cursor); Assert.assertEquals("value1", element.getParameterByName("param1").getValue()); Assert.assertEquals("value2", element.getParameterByName("param2").getValue()); Assert.assertNull(element.getParameterByName("param3")); try { element.getParameterByName(null); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { //expected } }
/** * Sets parameter of URI query overriding existing value if set. The parameter name and value * are expected to be unescaped and may contain non ASCII characters. * <p> * Please note query parameters and custom query component are mutually exclusive. This method * will remove custom query if present. * </p> * * @return this. */ public URIBuilder setParameter(final String param, final String value) { if (this.queryParams == null) { this.queryParams = new ArrayList<>(); } if (!this.queryParams.isEmpty()) { for (final Iterator<NameValuePair> it = this.queryParams.iterator(); it.hasNext(); ) { final NameValuePair nvp = it.next(); if (nvp.getName().equals(param)) { it.remove(); } } } this.queryParams.add(new BasicNameValuePair(param, value)); this.encodedQuery = null; this.encodedSchemeSpecificPart = null; this.query = null; return this; }
@Override public void processChallenge( final AuthChallenge authChallenge, final HttpContext context) throws MalformedChallengeException { this.paramMap.clear(); final List<NameValuePair> params = authChallenge.getParams(); if (params != null) { for (final NameValuePair param: params) { this.paramMap.put(param.getName().toLowerCase(Locale.ROOT), param.getValue()); } } this.complete = true; }
/** * Sets parameter of URI query overriding existing value if set. The parameter name and value * are expected to be unescaped and may contain non ASCII characters. * <p> * Please note query parameters and custom query component are mutually exclusive. This method * will remove custom query if present. * </p> * * @return this. */ public URIBuilder setParameter(final String param, final String value) { if (this.queryParams == null) { this.queryParams = new ArrayList<>(); } if (!this.queryParams.isEmpty()) { for (final Iterator<NameValuePair> it = this.queryParams.iterator(); it.hasNext(); ) { final NameValuePair nvp = it.next(); if (nvp.getName().equals(param)) { it.remove(); } } } this.queryParams.add(new BasicNameValuePair(param, value)); this.encodedQuery = null; this.encodedSchemeSpecificPart = null; this.query = null; return this; }
public String getBody() { final StringBuilder sb = new StringBuilder(); sb.append(this.value); for (int i = 0; i < this.parameters.size(); i++) { final NameValuePair parameter = this.parameters.get(i); sb.append("; "); sb.append(parameter.getName()); sb.append("=\""); sb.append(parameter.getValue()); sb.append("\""); } return sb.toString(); }
private static AuthChallenge createAuthChallenge(final ChallengeType challengeType, final String scheme, final List<NameValuePair> params) throws ParseException { if (scheme != null) { if (params.size() == 1) { final NameValuePair nvp = params.get(0); if (nvp.getValue() == null) { return new AuthChallenge(challengeType, scheme, nvp.getName(), null); } } return new AuthChallenge(challengeType, scheme, null, params.size() > 0 ? params : null); } if (params.size() == 1) { final NameValuePair nvp = params.get(0); if (nvp.getValue() == null) { return new AuthChallenge(challengeType, nvp.getName(), null, null); } } throw new ParseException("Malformed auth challenge"); }
private void moveAnyParametersInPathToQuery(final Map<String, Object> request) throws TestingFrameworkException { try { final String path = (String) request.get(PATH); if (path != null) { final URI uri = path.startsWith("/") ? new URI("http://localhost:8080" + path) : new URI("http://localhost:8080/"); final List<NameValuePair> params = URLEncodedUtils.parse(uri, StandardCharsets.UTF_8); @SuppressWarnings("unchecked") final Map<String, Object> queryMap = (Map<String, Object>) request.get(QUERY); for (final NameValuePair param : params) { queryMap.put(param.getName(), param.getValue()); } if (! params.isEmpty()) { request.put(PATH, uri.getPath()); } } } catch (final URISyntaxException e) { throw new TestingFrameworkException(e); } }
private void moveAnyParametersInPathToQuery(final Map<String, Object> request) throws TestingFrameworkException { try { final String path = (String) request.get(PATH); if (path != null) { final URI uri = path.startsWith("/") ? new URI("http://localhost:8080" + path) : new URI("http://localhost:8080/"); final List<NameValuePair> params = URLEncodedUtils.parse(uri, StandardCharsets.UTF_8); @SuppressWarnings("unchecked") final Map<String, Object> queryMap = (Map<String, Object>) request.get(QUERY); for (final NameValuePair param : params) { queryMap.put(param.getName(), param.getValue()); } if (! params.isEmpty()) { request.put(PATH, uri.getPath()); } } } catch (final URISyntaxException e) { throw new TestingFrameworkException(e); } }
private static ContentType create(final String mimeType, final NameValuePair[] params, final boolean strict) { Charset charset = null; if (params != null) { for (final NameValuePair param : params) { if (param.getName().equalsIgnoreCase("charset")) { final String s = param.getValue(); if (!TextUtils.isBlank(s)) { try { charset = Charset.forName(s); } catch (final UnsupportedCharsetException ex) { if (strict) { throw ex; } } } break; } } } return new ContentType(mimeType, charset, params != null && params.length > 0 ? params : null); }
private static ContentType create(final String mimeType, final NameValuePair[] params, final boolean strict) { Charset charset = null; if (params != null) { for (final NameValuePair param : params) { if (param.getName().equalsIgnoreCase("charset")) { final String s = param.getValue(); if (!TextUtils.isBlank(s)) { try { charset = Charset.forName(s); } catch (final UnsupportedCharsetException ex) { if (strict) { throw ex; } } } break; } } } return new ContentType(mimeType, charset, params != null && params.length > 0 ? params : null); }
@Override public void processChallenge( final AuthChallenge authChallenge, final HttpContext context) throws MalformedChallengeException { Args.notNull(authChallenge, "AuthChallenge"); this.paramMap.clear(); final List<NameValuePair> params = authChallenge.getParams(); if (params != null) { for (final NameValuePair param: params) { this.paramMap.put(param.getName().toLowerCase(Locale.ROOT), param.getValue()); } } if (this.paramMap.isEmpty()) { throw new MalformedChallengeException("Missing digest auth parameters"); } this.complete = true; }
/** * Creates a new instance with this MIME type and the given parameters. * * @param params * @return a new instance with this MIME type and the given parameters. * @since 4.4 */ public ContentType withParameters( final NameValuePair... params) throws UnsupportedCharsetException { if (params.length == 0) { return this; } final Map<String, String> paramMap = new LinkedHashMap<>(); if (this.params != null) { for (final NameValuePair param: this.params) { paramMap.put(param.getName(), param.getValue()); } } for (final NameValuePair param: params) { paramMap.put(param.getName(), param.getValue()); } final List<NameValuePair> newParams = new ArrayList<>(paramMap.size() + 1); if (this.charset != null && !paramMap.containsKey("charset")) { newParams.add(new BasicNameValuePair("charset", this.charset.name())); } for (final Map.Entry<String, String> entry: paramMap.entrySet()) { newParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } return create(this.getMimeType(), newParams.toArray(new NameValuePair[newParams.size()]), true); }
/** * Creates a new instance with this MIME type and the given parameters. * * @param params * @return a new instance with this MIME type and the given parameters. * @since 4.4 */ public ContentType withParameters( final NameValuePair... params) throws UnsupportedCharsetException { if (params.length == 0) { return this; } final Map<String, String> paramMap = new LinkedHashMap<>(); if (this.params != null) { for (final NameValuePair param: this.params) { paramMap.put(param.getName(), param.getValue()); } } for (final NameValuePair param: params) { paramMap.put(param.getName(), param.getValue()); } final List<NameValuePair> newParams = new ArrayList<>(paramMap.size() + 1); if (this.charset != null && !paramMap.containsKey("charset")) { newParams.add(new BasicNameValuePair("charset", this.charset.name())); } for (final Map.Entry<String, String> entry: paramMap.entrySet()) { newParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } return create(this.getMimeType(), newParams.toArray(new NameValuePair[newParams.size()]), true); }