[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[PATCH] Add various functions test in headers/headers.cpp, add testHeader.pl
From: Sergey Jukov <sergey@total-knowledge.com>
---
ChangeLog | 6 ++++++
headers/headers.cpp | 24 +++++++++++++++++++++++-
headers/headers.h | 14 +++++++++++++-
headers/testHeaders.pl | 40 ++++++++++++++++++++++++++++++++++++++++
4 files changed, 82 insertions(+), 2 deletions(-)
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/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