Re[2]: [Webtest] Request/response encoding problem

ilfrin webtest@lists.canoo.com
Mon, 23 Aug 2004 19:17:35 +0200


Hi Paul,

ok, problem solved ..

PK> You can change the character set used for encoding as follows:

PK> <config ...>
PK>    <option name="DefaultCharacterSet" value="ISO-8859-2"/>
PK> </config>

no, this does NOTHING .. the reason why is this code fragment from

com.canoo.webtest.steps.request.Target.java:

...
    private boolean tryCallingMethod(Header option, Class optionClass) {
        Class[] booleanClass = {boolean.class};
        Object[] booleanObject = {new Boolean("true".equalsIgnoreCase(option.getValue()))};
        try {
            Method method = optionClass.getDeclaredMethod("set" + option.getName(), booleanClass);
            if (method != null) 
                method.invoke(optionClass, booleanObject);
            log("set option <" + option.getName() + "> to value <" + option.getValue() + ">");
            return true;
        } catch (IllegalArgumentException e) {
            handleUnexpectedException(e);
        } catch (IllegalAccessException e) {
            handleUnexpectedException(e);
        } catch (InvocationTargetException e) {
            handleUnexpectedException(e);
        } catch (Exception e) { 
                // ignore
        }

        return false;
    }
...

so AFAIU only true/false values are accepted ..

so tracing our problem down we found that
com.meterware.httpunit.HttpUnitUtils has a not very pretty
implementation of decode:

    /**
     * Returns an interpretation of the specified URL-encoded string.
     * FIXME: currently assumes iso-8859-1 character set.
     **/
    public static String decode( String byteString ) {
        char[] chars = byteString.toCharArray();
        StringBuffer sb = new StringBuffer(chars.length);
        char[] hexNum = { '0', '0', '0' };

        int i = 0;
        while (i < chars.length) {
            if (chars[i] == '+') {
                i++;
                sb.append( ' ' );
            } else if (chars[i] == '%') {
                i++;
                hexNum[1] = chars[i++];
                hexNum[2] = chars[i++];
                sb.append( (char) Integer.parseInt( new String( hexNum ), 16 ) );
            } else {
                sb.append( chars[i++] );
            }
        }
        return sb.toString();
    }

 
so we changed it to:

    public static String decode( String byteString ) {
        try {
            return URLDecoder.decode(byteString,"ISO-8859-2");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

and the world is bright again :), I'll of course change the hardcoded
charset when I get the rest of the script working ...
    
    
-- 
Best regards
 ilfrin                            mailto:ilfrin@poczta.onet.pl