GistLabs / mechanize

mechanize for java
http://gistlabs.com/software/mechanize-for-java/
Mozilla Public License 2.0
78 stars 21 forks source link

relative Link's href() explodes #73

Open Vogel612 opened 10 years ago

Vogel612 commented 10 years ago

Any time one calls Link#href() on a relative link (e.g. <a href="/users/login" />) a MalformedURLException will get thrown in AbstractResource#absoluteUrl()

Code reproducing the problem:

        HtmlDocument chatLoginRootPage = agent.get("http://stackexchange.com/users/chat-login");
        Links daLinks = chatLoginRootPage.links();
        daLinks.getAll().forEach(e -> LOGGER.info(e.href()));

assumptions:

 MechanizeAgent agent = new MechanizeAgent();
 //redirect strategies for agent enabled
 private static final Logger LOGGER = java.util.Logger.getLogger(MinimalWorkingExample.class.getName());

Root cause in stack-trace (calling code omitted):

Caused by: java.net.MalformedURLException: no protocol: /users/login?returnurl=%2fusers%2fchat-login at java.net.URL.(URL.java:586) at java.net.URL.(URL.java:483) at java.net.URL.(URL.java:432) at com.gistlabs.mechanize.AbstractResource.absoluteUrl(AbstractResource.java:237) ... 8 more

fge commented 10 years ago

Well, the problem here is with HtmlDocument isn't it? Either itself or its usage. Its .href() method only contains the path, query (and fragment?) elements of the URI, not its protocol or host.

Vogel612 commented 10 years ago

On a sidenote, this is also happening with forms referencing relative URLs when they are submitted