Path: blob/master/test/jdk/sun/net/www/HeaderTests.java
41149 views
/*1* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @modules java.base/sun.net.www26* @summary Tests for HeaderParser and MessageHeader27*28* Test of HeaderParser, MessageHeader changes29* introduced for bug fix 4722333.30*/3132import sun.net.www.HeaderParser;33import sun.net.www.MessageHeader;34import java.io.*;35import java.util.Iterator;3637public class HeaderTests {3839static MessageHeader createMessageHeader (String s) {40ByteArrayInputStream bis = new ByteArrayInputStream (s.getBytes());41MessageHeader h = new MessageHeader ();42try {43h.parseHeader (bis);44} catch (IOException e) {45throw new RuntimeException ("IOException parsing header");46}47return h;48}4950/* String to parse */5152static String s1 =53"Foo: bar\r\n"+54"Fub: abc\r\n"+55"Foo:\r\n"+56"Fub: \r\n"+57"Foo: param1=one param2=\"two\" param3 param4=\"value=4\"\r\n"+58"Fub: xparam1=one xparam2=\"two\" xparam3 xparam4=\"value=4\"\r\n";5960static String s2 = "p1=1 p2=2 p3=3 p4=4 p5=5 p6=\"six\" p7=7 p8=8 p9=9 p10=10 p11=11 p12=12";6162static String s3 = "p1=1, p2=2, p3=3, p4=4, p5=5, p6=\"six\", p7=7, p8=8, p9=9, p10=10, p11=11, p12=12";6364static String s23_expect[][] = {{"p2","2"},{"p3","3"},{"p4","4"},{"p5","5"},{"p6","six"},65{"p7","7"},{"p8","8"},{"p9","9"},{"p10","10"},{"p11","11"},{"p12","12"}};6667static String s23_expect1[][] = {{"p1","1"},{"p2","2"},{"p3","3"},{"p4","4"},{"p5","5"},{"p6","six"},68{"p7","7"},{"p8","8"},{"p9","9"},{"p10","10"},{"p11","11"}};6970static String s23_expect2[][] = {{"p5","5"},{"p6","six"}};7172/* Expected header parser contents for Foo headers */73static String s1_Foo[][][] = {{{"bar", null}74},75{{"param1","one"},{"param2","two"},76{"param3", null},{"param4","value=4"}77}78};7980/* Expected header parser contents for Fub headers */81static String s1_Fub[][][] = {{{"abc", null}82},83{{"xparam1","one"},{"xparam2","two"},84{"xparam3", null},{"xparam4","value=4"}85}86};8788public static void main (String args[]) {89MessageHeader head = createMessageHeader (s1);90Iterator iter = head.multiValueIterator ("Foo");91checkHeader (iter, s1_Foo);92iter = head.multiValueIterator ("Fub");93checkHeader (iter, s1_Fub);9495HeaderParser hp = new HeaderParser (s2).subsequence (1,12);96check (hp, s23_expect);9798hp = new HeaderParser (s3).subsequence (1,12);99check (hp, s23_expect);100101hp = new HeaderParser (s3).subsequence (0,11);102check (hp, s23_expect1);103104hp = new HeaderParser (s2).subsequence (4,6);105check (hp, s23_expect2);106}107108static void checkHeader (Iterator iter, String[][][] expect) {109for (int i=0; iter.hasNext (); ) {110String s = (String) iter.next();111HeaderParser p = new HeaderParser (s);112boolean empty = check (p, expect[i]);113if (!empty) {114i++;115}116}117}118119static boolean check (HeaderParser p, String[][]expect) {120Iterator keys = p.keys();121Iterator vals = p.values();122boolean empty = true;123for (int j=0; keys.hasNext(); j++) {124empty = false;125String key = (String)keys.next();126String ival = (String)vals.next();127String val = p.findValue (key);128if (val == null && ival == null)129continue;130if (!val.equals(ival)) {131throw new RuntimeException ("Error " + val + "/" + ival);132}133if (!expect[j][0].equals (key)) {134throw new RuntimeException ("Error "+key+"/" + expect[j][0]);135}136if (expect[j][1] != null && !expect[j][1].equals (val)) {137throw new RuntimeException ("Error "+val+"/" + expect[j][1]);138}139}140return empty;141}142}143144145