Welcome, guest | Sign In | My Account | Store | Cart

This script performs an automated form login against a site protected by the J2EE j_security_check model.

Tcl, 213 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package require TclCurl

proc getUrl {curlHandle url} {

   puts                "\n ### getUrl using $url\n"

   set userAgent "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90)" 

   puts                "\nuserAgent = $userAgent\n" 

   $curlHandle configure   -url               $url \
			   -bodyvar           body \
			   -headervar         headers \
			   -failonerror       1 \
			   -followlocation    1 \
			   -sslverifypeer     0  \
			   -useragent         $userAgent \
			   -errorbuffer       errorBuffer   

   if { [ catch {$curlHandle perform } r ] == 0} {

	set  httpCode      [$curlHandle getinfo httpcode]
	set  contentType   [$curlHandle getinfo contenttype]   
	set  redirectCount [$curlHandle getinfo redirectcount]
	set  fileTime      [$curlHandle getinfo filetime]      
	set  effUrl        [$curlHandle getinfo effectiveurl] 
	set  totalTime     [$curlHandle getinfo totaltime]     

	foreach { 1 2 } [ array get headers ] {
	    puts                [ format "%-20s = %-20s" $1 $2 ]
	    if { [ regexp -nocase "location" $1 ] == 1 }   { set url $2 }
	    if { [ regexp -nocase "Set-Cookie" $1 ] == 1 } { set cookie $2 }
	}

	puts                "\nLast effective URL = $url"

	puts                "\nCookie returned    = $cookie"
  
	return $cookie

      } else {
	  puts "ERROR1"
	  return -code error $errorBuffer
   }
}

proc postUrl {curlHandle url postString cookie} {

   puts                "\n postString is $postString"

   puts                "\n ### postUrl using $url\n"

   puts                "\n ### cookie  is    $cookie\n"

   set userAgent "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90)" 

   puts "userAgent = $userAgent" 

   $curlHandle configure -url               $url \
          		 -cookie            $cookie \
          		 -bodyvar           body \
          		 -headervar         headers \
          		 -postfields        $postString \
          		 -useragent         $userAgent \
          		 -failonerror       1 \
          		 -errorbuffer       errorBuffer   

   if { [ catch {$curlHandle perform } r ] == 0} {

	set  httpCode      [$curlHandle getinfo httpcode]
	set  contentType   [$curlHandle getinfo contenttype]   
	set  redirectCount [$curlHandle getinfo redirectcount]
	set  fileTime      [$curlHandle getinfo filetime]      
	set  effUrl        [$curlHandle getinfo effectiveurl] 
	set  totalTime     [$curlHandle getinfo totaltime]     

	foreach { 1 2 } [ array get headers ] {
	    puts                [ format "%-20s = %-20s" $1 $2 ]
	    if { [ regexp -nocase "location" $1 ] == 1 }   { set url $2 }
	    if { [ regexp -nocase "Set-Cookie" $1 ] == 1 } { set cookie $2 }
	}

	puts                "\nLast effective URL = $url"

	puts                "\nCookie returned    = $cookie"
  
	return $cookie

      } else {
	  puts "ERROR1"
	  return -code error $errorBuffer
      }

}

proc getUrlCookie {curlHandle url cookie} {

   puts "\n ### getUrl using $url\n"
   puts "\n ### cookie  is    $cookie\n"

   set userAgent "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90)" 

   puts "\nuserAgent = $userAgent\n" 

   $curlHandle configure   -url               $url \
			   -bodyvar           body \
			   -headervar         headers \
			   -failonerror       1 \
			   -followlocation    1 \
			   -sslverifypeer     0  \
          		   -cookie            $cookie \
			   -useragent         $userAgent \
			   -errorbuffer       errorBuffer   

   if { [ catch {$curlHandle perform } r ] == 0} {

	set  httpCode      [$curlHandle getinfo httpcode]
	set  contentType   [$curlHandle getinfo contenttype]   
	set  redirectCount [$curlHandle getinfo redirectcount]
	set  fileTime      [$curlHandle getinfo filetime]      
	set  effUrl        [$curlHandle getinfo effectiveurl] 
	set  totalTime     [$curlHandle getinfo totaltime]     

	foreach { 1 2 } [ array get headers ] {
	    puts                [ format "%-20s = %-20s" $1 $2 ]
	    if { [ regexp -nocase "location" $1 ] == 1 }   { set url $2 }
	    if { [ regexp -nocase "Set-Cookie" $1 ] == 1 } { set cookie $2 }
	}

	puts                "\nLast effective URL = $url"

	puts                "\nCookie returned    = $cookie"

  
	return $body

      } else {
	  set  httpCode      [$curlHandle getinfo httpcode]

	  puts "ERROR: $httpCode"

	  puts $errorBuffer
	  return -code error $errorBuffer
   }
}

######################################
# Set Variables
######################################

set curlHandle [curl::init]

#####################################################
# get initial url(login page) and return cookie.
# set url https://www.yoursite.com/yoursite/login.jsp
#####################################################

set url https://www.yoursite.com/yoursite/login.jsp

if {[catch {getUrl $curlHandle $url} r] == 0} {
     set cookie $r
     } else {
        puts "ERROR:"
        puts $r 
        $curlHandle cleanup
	exit 1
}

#######################################################################
#post userid and password with sessionid to login page 
#set url https://www.yoursite.com/yoursite/j_security_check 
#######################################################################

set url https://www.yoursite.com/yoursite/j_security_check 
set postString "j_username=abcde&j_password=12345&submit=logon" 

if {[catch {postUrl $curlHandle $url $postString $cookie} r] == 0} {
     set cookie $r
     } else {
        puts "ERROR:"
        puts $r 

        $curlHandle cleanup
	exit 1
}

#######################################################################
#get protected url(page)
#set url https://www.yoursite.com/yoursite/member/
#######################################################################

set url https://www.yoursite.com/yoursite/member/

if {[catch {getUrlCookie $curlHandle $url $cookie} r] == 0} {
     set body $r
     if {[regexp -nocase {\<title\>Members Area\<\/title\>} $body] ==1} {
	 set continue true 
     } else {
	    puts "ERROR:"
	    puts $r 

	    $curlHandle cleanup
	    exit 1
     }
} else {
	puts "ERROR:"
	puts $r 

	$curlHandle cleanup
	exit 1
}

$curlHandle cleanup

Automated regression test of site login.

Created by Patrick Finnegan on Tue, 29 Jul 2003 (MIT)
Tcl recipes (162)
Patrick Finnegan's recipes (56)

Required Modules

  • (none specified)

Other Information and Tasks