CPPSERV


Home Projects Jobs Clientele Contact

cppserv


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Add test functionality for various functions in headers/headers.cpp,



From: Sergey Jukov <sergey@total-knowledge.com>

diff --git a/ChangeLog b/ChangeLog
index 0dac80a..2080ab4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Sergey Jukov <sergey@total-knowledge.com>            Mon, 18 Sep 2006 12:20:00 -0800
+- Add testing getHeader() and getDateHeader() functionality for
+  headers.cpp and headers.h. Add testHeaders.pl for testing date header parser.
+- Add error handling in headers.cpp for getDateHeader().
+- Add containsHeader(), setDateHeader testing in headers.
+	
 Ilya A. Volynets-Evenbakh <ilya@total-knowledge.com> Fri, 13 Sep 2006 14:22:11 -0800
 - Fix few copyright headers
 
diff --git a/Makefile.adon b/Makefile.adon
index 4dbcc31..b477c92 100644
--- a/Makefile.adon
+++ b/Makefile.adon
@@ -1,2 +1,2 @@
-ADON_SUBDIRS := csp-store hello utils fileupload redirect cookies session headers sharedata params BooksODBC database index
+ADON_SUBDIRS := csp-store hello attributes utils fileupload redirect cookies session headers sharedata params BooksODBC database index
 EXTRA_DIST := README engine.xml
diff --git a/engine.xml b/engine.xml
index 4dec2a8..5105f49 100644
--- a/engine.xml
+++ b/engine.xml
@@ -5,6 +5,7 @@
 <app name="test">
     <servlet name="IndexServlet" dso="./debug/index/IndexServlet.so"/>
     <servlet name="HelloServlet" dso="./debug/hello/HelloServlet.so"/>
+    <servlet name="AttributesServlet" dso="./debug/attributes/AttributesServlet.so"/>
     <servlet name="FileUploadServlet" dso="./debug/fileupload/FileUploadServlet.so"/>
     <servlet name="RedirectServlet" dso="./debug/redirect/RedirectServlet.so"/>
     <servlet name="CookiesServlet" dso="./debug/cookies/CookiesServlet.so"/>
diff --git a/env-tests/env-test-custom.mk b/env-tests/env-test-custom.mk
index 81efa07..9a8021d 100644
--- a/env-tests/env-test-custom.mk
+++ b/env-tests/env-test-custom.mk
@@ -1,5 +1,5 @@
 #####################################################
-ifneq ($(strip $(shell $(CXX) -v 2>&1| grep '^gcc')),)
+#ifneq ($(strip $(shell $(CXX) -v 2>&1| grep '^gcc')),)
 $(eval $(call cxx_flag_kirpich,-W))
 $(eval $(call cxx_flag_kirpich,-Wall))
 $(eval $(call cxx_flag_kirpich,-Wextra))
@@ -11,5 +11,5 @@ #$(eval $(call cxx_flag_kirpich,-pedanti
 $(eval $(call sptk_namespace_kirpich))
 $(eval $(call sptk_odbc_kirpich))
 $(eval $(call sptk_sqlite_kirpich))
-endif
+#endif
 #####################################################
diff --git a/headers/headers.cpp b/headers/headers.cpp
index 4a253c8..9b49a95 100644
--- a/headers/headers.cpp
+++ b/headers/headers.cpp
@@ -18,13 +18,17 @@
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 #include "headers.h"
+#include <iostream>
+#include <sstream>
+#include <servlet/ServletException.h>
 
 void HeadersServlet::service(servlet::HttpServletRequest& req, servlet::HttpServletResponse& resp)
 {
 	std::ostream &out = resp.getOutputStream();
 	std::auto_ptr< std::vector<std::string> > headerNames = req.getHeaderNames();
-	resp.setHeader("Pragma", "no-cache");
 	resp.setHeader("X-Test-Header", "MyHeader");
+	long seconds = time(NULL) + 3600;
+	resp.setDateHeader("expires", seconds);
 	renderHeader(out);
 	out<<"<PRE>";
 	for(std::vector<std::string>::iterator it = headerNames->begin();
@@ -36,6 +40,24 @@ void HeadersServlet::service(servlet::Ht
 		    out<<*it<<':'<<*vit<<std::endl;
 	}
 	out<<"============\nProtocol="<<req.getProtocol()<<std::endl;
+	out << "\n==========\nHTTP Method = "+req.getMethod();
+	std::string testHeaderName = "if-modified-since";
+	out<<"\n"<<testHeaderName+":"+req.getHeader(testHeaderName); 
+	try {
+	        long l = req.getDateHeader(testHeaderName);
+		std::stringstream s;
+		s<<l;
+		std::string dateHeader = s.str();
+		out<<"\n"<<testHeaderName+" value = "+dateHeader; 
+	} catch (servlet::ServletException e) {
+	        out<<"\n"<<testHeaderName+" is not formatted correctly";
+	}
+	std::string respTestHeaderName = "X-Test-Header";
+	if(resp.containsHeader(respTestHeaderName)) {
+	         out<<"\n"<<respTestHeaderName<<" is set";	  
+	} else {
+	         out<<"\n"<<respTestHeaderName<<" is not set";	  
+	}
 	out<<"</PRE>";
 	renderFooter(out);
 }
diff --git a/headers/headers.h b/headers/headers.h
index 8bf8b31..279c695 100644
--- a/headers/headers.h
+++ b/headers/headers.h
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2004-2005 by Ilya A. Volynets-Evenbakh                  *
+ *   Copyright (C) 2004-2006 by Ilya A. Volynets-Evenbakh                  *
  *   ilya@total-knowledge.com                                              *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -30,6 +30,18 @@ This servlet demonstrates how to set res
 at request headers.
 It displays a page, with values of all request headers,
 and sends X-Test-Header: header. It also sets Pragma: no-cache
+It displays HTTP method with which this request was made, work of getHeader()
+which returns the value of specified request header(null if there was no such 
+header in HTTP request) and getDateHeader() which returns the number of 
+milliseconds since January 1, 1970 GMT, or -1 if the named header was not 
+included with the request. Throws IllegalArgumentException if the header value
+can't be converted to a date.
+Example provided for If-Modified-Since HTTP request header using testHeaders.pl
+It displays work of containsHeader(), returns true if the named response header
+has already been set, false otherwise. Example provided for X-Test-Header test
+header.  
+It demonstrates work of setDateHeader() by setting "expires" response header to
+the value greater then "modified" response header value to 1 hour.
 
 @author Ilya A. Volynets-Evenbakh
 */
diff --git a/headers/testHeaders.pl b/headers/testHeaders.pl
new file mode 100644
index 0000000..c5cf559
--- /dev/null
+++ b/headers/testHeaders.pl
@@ -0,0 +1,40 @@
+use LWP::UserAgent;
+$ua = LWP::UserAgent->new;
+
+my $req = HTTP::Request->new(POST => 'http://localhost/~sergey/csp/HeadersServlet');
+$req->content_type('application/x-www-form-urlencoded');
+$req->content('key=value');
+
+my $headerName = "If-Modified-Since";
+
+my @headerValues = (
+'Tue, 9 Sep 2006 12:45:26 GMT',
+'Mon, 33 Sep 06 12:00:00 GMT',
+''
+);
+
+
+
+foreach $headerValue (@headerValues)
+{
+
+    $req->header($headerName => $headerValue);
+
+    my $res = $ua->request($req);
+
+    if ($res->is_success) {
+
+	print $res->content."\n\n------------------------------------------------------\n\n\n";
+
+    }
+    else {
+	print $res->status_line, "\n";
+    }
+
+}
+
+
+
+
+
+
-- 
1.4.2


Authoright © Total Knowledge: 2001-2008