Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

WorkItem: 4707 Testing class (not JUnit) for the security interceptor

  • Loading branch information...
commit 2d18192327e28ef0edf1d1782b978f7aeb9fac1c 1 parent 8909f31
Jennifer Baldwin jebaldwin authored
4 .classpath
@@ -21,6 +21,10 @@
21 21 <classpathentry kind="lib" path="lib/hibernate-jpa-2.0-api-1.0.0.Final.jar"/>
22 22 <classpathentry kind="lib" path="lib/cglib-2.2.jar"/>
23 23 <classpathentry kind="lib" path="lib/poi-3.6.jar"/>
  24 + <classpathentry kind="lib" path="lib/httpclient-4.1.3.jar"/>
  25 + <classpathentry kind="lib" path="lib/httpclient-cache-4.1.3.jar"/>
  26 + <classpathentry kind="lib" path="lib/httpcore-4.1.4.jar"/>
  27 + <classpathentry kind="lib" path="lib/httpmime-4.1.3.jar"/>
24 28 <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
25 29 <classpathentry exported="true" kind="lib" path="lib/commons-logging.jar">
26 30 <attributes>
BIN  lib/httpclient-4.1.3.jar
Binary file not shown
BIN  lib/httpclient-cache-4.1.3.jar
Binary file not shown
BIN  lib/httpcore-4.1.4.jar
Binary file not shown
BIN  lib/httpmime-4.1.3.jar
Binary file not shown
65 test/fi/hut/soberit/agilefant/web/SecurityInterceptorTest.java
... ... @@ -0,0 +1,65 @@
  1 +package fi.hut.soberit.agilefant.web;
  2 +
  3 +import java.io.IOException;
  4 +import java.util.ArrayList;
  5 +import java.util.List;
  6 +
  7 +import org.apache.http.HttpResponse;
  8 +import org.apache.http.NameValuePair;
  9 +import org.apache.http.client.ClientProtocolException;
  10 +import org.apache.http.client.CookieStore;
  11 +import org.apache.http.client.entity.UrlEncodedFormEntity;
  12 +import org.apache.http.client.methods.HttpPost;
  13 +import org.apache.http.impl.client.DefaultHttpClient;
  14 +import org.apache.http.message.BasicNameValuePair;
  15 +import org.apache.http.protocol.HTTP;
  16 +
  17 +public class SecurityInterceptorTest{
  18 +
  19 + private static DefaultHttpClient httpclient = new DefaultHttpClient();
  20 + private static DefaultHttpClient uhttpclient = new DefaultHttpClient();
  21 +
  22 + public static void main(String[] args) {
  23 + try{
  24 + testSetAdmin();
  25 + } catch(Exception e){
  26 + e.printStackTrace();
  27 +
  28 + // When HttpClient instance is no longer needed,
  29 + // shut down the connection manager to ensure
  30 + // immediate deallocation of all system resources
  31 + httpclient.getConnectionManager().shutdown();
  32 + uhttpclient.getConnectionManager().shutdown();
  33 + }
  34 + }
  35 +
  36 + private static void testSetAdmin() throws ClientProtocolException, IOException {
  37 +
  38 + HttpPost httpost = new HttpPost("http://localhost:8080/agilefant/j_spring_security_check");
  39 +
  40 + List <NameValuePair> nvps = new ArrayList <NameValuePair>();
  41 + nvps.add(new BasicNameValuePair("j_username", "yourlogin"));
  42 + nvps.add(new BasicNameValuePair("j_password", "yourpassword"));
  43 +
  44 + httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  45 + HttpResponse response = httpclient.execute(httpost);
  46 +
  47 + System.out.println("Login form status: " + response.getStatusLine());
  48 + CookieStore cookies = httpclient.getCookieStore();
  49 +
  50 + HttpPost uhttpost = new HttpPost("http://localhost:8080/agilefant/ajax/storeUser.action");
  51 +
  52 + List <NameValuePair> unvps = new ArrayList <NameValuePair>();
  53 + unvps.add(new BasicNameValuePair("userId", "36"));
  54 + unvps.add(new BasicNameValuePair("user.admin", "true"));
  55 +
  56 + uhttpost.setEntity(new UrlEncodedFormEntity(unvps, HTTP.UTF_8));
  57 +
  58 + uhttpclient.setCookieStore(cookies);
  59 + HttpResponse response2 = uhttpclient.execute(uhttpost);
  60 +
  61 + System.out.println("Change admin status: " + response2.getStatusLine());
  62 + }
  63 +
  64 +
  65 +}

0 comments on commit 2d18192

Please sign in to comment.
Something went wrong with that request. Please try again.