org.josso.test.tck.Tck002Test.java Source code

Java tutorial

Introduction

Here is the source code for org.josso.test.tck.Tck002Test.java

Source

/*
 * JOSSO: Java Open Single Sign-On
 *
 * Copyright 2004-2009, Atricore, Inc.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 *
 */

package org.josso.test.tck;

import org.josso.test.support.TckTestSupport;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.junit.Test;

/**
 * This will attempt a basic login by accessing a protected resource in a partner application using a Generic Servlet Agent
 * All redirects are followed by the HttpClient instance, we only check some values that must appear in response bodies.
 *
 * @author <a href="mailto:sognzalez@josso.org">Sebastian Gonzalez Oyuela</a>
 */
public class Tck002Test extends TckTestSupport {

    public static final Log log = LogFactory.getLog(Tck002Test.class);

    @Test
    public void runTck() throws Exception {
        doGetProtectedDelegated();
        doPostCredentials();
    }

    protected void doGetProtectedDelegated() throws Exception {
        // This will be redirected until the gateway login form is presented
        HttpMethod getMethod = doGet(
                "http://localhost:" + getProperty("josso.tck.serverPort") + "/webapp1/protected-delegated.jsp");
        String body = getMethod.getResponseBodyAsString();

        assert body.indexOf("josso_username") > 0 : "No 'josso_username' field received in response";
        assert body.indexOf("josso_password") > 0 : "No 'josso_password' field received in response";

    }

    protected void doPostCredentials() throws Exception {
        NameValuePair username = new NameValuePair("josso_username", "user1");
        NameValuePair password = new NameValuePair("josso_password", "user1pwd");
        NameValuePair cmd = new NameValuePair(PARAM_JOSSO_CMD, "login");

        PostMethod postMethod = doPost("http://localhost:" + getProperty("josso.tck.serverPort")
                + "/josso/signon/usernamePasswordLogin.do", username, password, cmd);
        int status = postMethod.getStatusCode();
        assert status == HttpStatus.SC_MOVED_TEMPORARILY : "Unexpected HTTP status " + status;

        Header location = postMethod.getResponseHeader("Location");
        location.getValue();
        HttpMethod getMethod = doGet(location.getValue());
        status = getMethod.getStatusCode();

        assert status == HttpStatus.SC_OK : "Unexpected HTTP status " + status;

        String body = getMethod.getResponseBodyAsString();
        assert body.indexOf("JOSSO_SESSIONID=") > 0 : "No JOSSO_SESSIONID= recived in response";
    }

    protected String getTckId() {
        return "002";
    }
}