|
Java example source code file (TestURLEncodedUtils.java)
The TestURLEncodedUtils.java example source code/* * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.http.client.utils; import java.net.URI; import java.util.ArrayList; import java.util.List; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import org.apache.http.NameValuePair; import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; public class TestURLEncodedUtils extends TestCase { public TestURLEncodedUtils(final String testName) { super(testName); } public static void main(String args[]) { String[] testCaseName = { TestURLEncodedUtils.class.getName() }; junit.textui.TestRunner.main(testCaseName); } public static Test suite() { return new TestSuite(TestURLEncodedUtils.class); } public void testParseURI () throws Exception { List <NameValuePair> result; result = parse("", null); assertTrue(result.isEmpty()); result = parse("Name1=Value1", null); assertEquals(1, result.size()); assertNameValuePair(result.get(0), "Name1", "Value1"); result = parse("Name2=", null); assertEquals(1, result.size()); assertNameValuePair(result.get(0), "Name2", null); result = parse("Name3", null); assertEquals(1, result.size()); assertNameValuePair(result.get(0), "Name3", null); result = parse("Name4=Value+4%21", null); assertEquals(1, result.size()); assertNameValuePair(result.get(0), "Name4", "Value 4!"); result = parse("Name4=Value%2B4%21", null); assertEquals(1, result.size()); assertNameValuePair(result.get(0), "Name4", "Value+4!"); result = parse("Name4=Value+4%21+%214", null); assertEquals(1, result.size()); assertNameValuePair(result.get(0), "Name4", "Value 4! !4"); result = parse("Name5=aaa&Name6=bbb", null); assertEquals(2, result.size()); assertNameValuePair(result.get(0), "Name5", "aaa"); assertNameValuePair(result.get(1), "Name6", "bbb"); result = parse("Name7=aaa&Name7=b%2Cb&Name7=ccc", null); assertEquals(3, result.size()); assertNameValuePair(result.get(0), "Name7", "aaa"); assertNameValuePair(result.get(1), "Name7", "b,b"); assertNameValuePair(result.get(2), "Name7", "ccc"); result = parse("Name8=xx%2C++yy++%2Czz", null); assertEquals(1, result.size()); assertNameValuePair(result.get(0), "Name8", "xx, yy ,zz"); } public void testParseEntity () throws Exception { final StringEntity entity = new StringEntity("Name1=Value1", null); entity.setContentType(URLEncodedUtils.CONTENT_TYPE); final List <NameValuePair> result = URLEncodedUtils.parse(entity); assertEquals(1, result.size()); assertNameValuePair(result.get(0), "Name1", "Value1"); entity.setContentType("text/test"); assertTrue(URLEncodedUtils.parse(entity).isEmpty()); } static final int SWISS_GERMAN_HELLO [] = { 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 }; static final int RUSSIAN_HELLO [] = { 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 }; private static String constructString(int [] unicodeChars) { StringBuffer buffer = new StringBuffer(); if (unicodeChars != null) { for (int i = 0; i < unicodeChars.length; i++) { buffer.append((char)unicodeChars[i]); } } return buffer.toString(); } public void testParseUTF8Entity () throws Exception { String ru_hello = constructString(RUSSIAN_HELLO); String ch_hello = constructString(SWISS_GERMAN_HELLO); List <NameValuePair> parameters = new ArrayList Other Java examples (source code examples)Here is a short list of links related to this Java TestURLEncodedUtils.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.