Friday, June 21, 2013

Re: adding basic auth to GWTTestCase?

To answer my own question and for posterity.
In order to provide a username/password to the GWTTestCase, I created my own run style
that extended the RunStyleHtmlUnit class and then added the -runStyle Samp to the gwt.args.


package com.google.gwt.junit;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
import com.gargoylesoftware.htmlunit.WebClient;
import com.google.gwt.core.ext.TreeLogger;

/**
 * Launches a web-mode test via HTMLUnit.
 */
public class RunStyleSamp extends RunStyleHtmlUnit
{
    /**
     * Runs HTMLUnit in a separate thread.
     */
    protected static class SampHtmlUnitThread extends HtmlUnitThread
    {

        public SampHtmlUnitThread(
                final BrowserVersion browser,
                final String url,
                final TreeLogger treeLogger,
                final boolean developmentMode)
        {
            super(browser, url, treeLogger, developmentMode);
        }

        @Override
        protected void setupWebClient(final WebClient webClient)
        {
            super.setupWebClient(webClient);

            // add authentication here
            final DefaultCredentialsProvider credentialsProvider = new DefaultCredentialsProvider();
            credentialsProvider.addCredentials(username, password);
            webClient.setCredentialsProvider(credentialsProvider);
        }
    }

    private static String username = "planner1";

    private static String password = "Test777Test";

    private boolean devMode;

    /**
     * Create a RunStyle instance with the passed-in browser targets.
     */
    public RunStyleSamp(final JUnitShell shell)
    {
        super(shell);
    }

    @Override
    protected HtmlUnitThread createHtmlUnitThread(final BrowserVersion browser, final String url)
    {
        return new SampHtmlUnitThread(browser, url, shell.getTopLogger().branch(
                TreeLogger.SPAM,
                "logging for HtmlUnit thread"), devMode);
    }

    @Override
    public boolean setupMode(final TreeLogger logger, final boolean developmentMode)
    {
        this.devMode = developmentMode;
        return super.setupMode(logger, developmentMode);
    }
}



I suppose I could get fancy and define the username/password as -D properties
or arguments to the runStyle maybe, but this is as far as I'm going.  

It really seems like this should be a standard feature, IMHO.




--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment