[z3-checkins] r33001 - in z3/jsonserver/trunk: . tests
jwashin at codespeak.net
jwashin at codespeak.net
Sun Oct 8 16:09:02 CEST 2006
Author: jwashin
Date: Sun Oct 8 16:08:59 2006
New Revision: 33001
Added:
z3/jsonserver/trunk/tests/test_sum_form.pt
Modified:
z3/jsonserver/trunk/JSONViews.txt
z3/jsonserver/trunk/ftests.py
Log:
added a POST test to JSONViews.txt
Modified: z3/jsonserver/trunk/JSONViews.txt
==============================================================================
--- z3/jsonserver/trunk/JSONViews.txt (original)
+++ z3/jsonserver/trunk/JSONViews.txt Sun Oct 8 16:08:59 2006
@@ -10,8 +10,8 @@
We'll follow the xmlrpc README to demonstrate.
First, write a view class, descended from JSONView. Whatever is returned in the
-doResponse method is what will be sent as a response. The usual view instance
-variables context and request are available.
+doResponse method is what will be sent as a response. The usual view instance
+variables, context and request, are available.
>>> from jsonserver import JSONView
>>> class FolderListing(JSONView):
@@ -39,27 +39,21 @@
... </configure>
... """)
-Exactly like the xmlrpc example, we add some items to the root folder.
-
- >>> print http(r"""
- ... POST /@@contents.html HTTP/1.1
- ... Authorization: Basic bWdyOm1ncnB3
- ... Content-Length: 73
- ... Content-Type: application/x-www-form-urlencoded
- ...
- ... type_name=BrowserAdd__zope.app.folder.folder.Folder&new_value=f1""")
- HTTP/1.1 303 See Other
- ...
+Let's set up a browser.
+ >>> from zope.testbrowser.testing import Browser
+ >>> browser = Browser('http://localhost/@@/testbrowser/simple.html')
+ >>> #N.B.,this was how I figured out the need for the IJSONWriter utility...
+ >>> #browser.handleErrors = False
+ >>> browser.addHeader('Authorization','Basic mgr:mgrpw')
- >>> print http(r"""
- ... POST /@@contents.html HTTP/1.1
- ... Authorization: Basic bWdyOm1ncnB3
- ... Content-Length: 73
- ... Content-Type: application/x-www-form-urlencoded
- ...
- ... type_name=BrowserAdd__zope.app.folder.folder.Folder&new_value=f2""")
- HTTP/1.1 303 See Other
- ...
+Almost exactly like the xmlrpc example, we add some items to the root folder.
+ >>> typeName = "BrowserAdd__zope.app.folder.folder.Folder"
+ >>> newValue = 'f1'
+ >>> browser.open('/@@contents.html?type_name=%s&new_value=%s' % (typeName,
+ ... newValue))
+ >>> newValue = 'f2'
+ >>> browser.open('/@@contents.html?type_name=%s&new_value=%s' % (typeName,
+ ... newValue))
Before we can JSONView, there needs to be an IJSONWriter utility. Here's one.
>>> import zope.component
@@ -67,11 +61,8 @@
>>> from jsonserver.interfaces import IJSONWriter
>>> zope.component.provideUtility(jsoncomponent.JSONWriter(),IJSONWriter)
-Let's set up a browser.
- >>> from zope.testbrowser.testing import Browser
+We reset the browser just for fun...
>>> browser = Browser('http://localhost/@@/testbrowser/simple.html')
- >>> #this was how I figured out the need for the IJSONWriter utility...
- >>> #browser.handleErrors = False
Now, we can call our new JSONView and get a response:
>>> browser.open('/folderlist')
@@ -91,8 +82,8 @@
Pretty cool, yes? This is much smaller than a similar xmlrpc response.
But what about parameters? Let's create another class with some error handling.
-There's no real standard on this, so you may need to commune with the cliemt
-implementation to see how to handle errors.
+There's no real standard on error handling in JSON, so you may need to commune
+with the client implementation to see how to handle errors.
>>> import decimal
>>> class FolderStupidSum(JSONView):
... """return two values and their sum"""
@@ -105,7 +96,11 @@
... return {'error':'bad params','a':a, 'b':b}
... return {'a':a,'b':b,'sum':a+b}
-and register it.
+First, a hack to make the page template file findable, then register the sum
+page and a sum_form page.
+ >>> import os
+ >>> loc = os.path.dirname(__file__)
+ >>> pt = os.path.join(loc,'tests','test_sum_form.pt')
>>> from zope.configuration import xmlconfig
>>> ignored = xmlconfig.string("""
... <configure
@@ -120,15 +115,21 @@
... class="jsonserver.JSONViews.FolderStupidSum"
... permission="zope.ManageContent"
... />
+ ... <browser:page
+ ... name="sum_form.html"
+ ... for="zope.app.folder.folder.IFolder"
+ ... template="%s"
+ ... permission="zope.ManageContent"
+ ... />
... </configure>
- ... """)
+ ... """ % pt)
Start a new browser.
>>> browser = Browser('http://localhost/@@/testbrowser/simple.html')
>>> #browser.handleErrors = False
>>> browser.addHeader('Authorization','Basic mgr:mgrpw')
-Let's do a couple of views. browser is already authenticated. Asssure the
+Let's do a couple of views. Browser is already authenticated. Asssure the
parameters in the GET match the names in the doResponse method. Default values
are OK, and probably a good idea.
>>> browser.open('/sum?a=5')
@@ -160,3 +161,15 @@
>>> 'bad params' in browser.contents
True
+Now, let's open the test form in the browser so that we can see if POST works.
+Ordinarily, a POST for a JSONView would be done in an XHR, but we are just
+testing functionality here.
+ >>> browser.open('/sum_form.html')
+ >>> a = browser.getControl(name='a')
+ >>> b = browser.getControl(name='b')
+ >>> a.value="34"
+ >>> b.value="66"
+ >>> submit = browser.getControl(name="submit")
+ >>> submit.click()
+ >>> '"sum":100' in browser.contents
+ True
Modified: z3/jsonserver/trunk/ftests.py
==============================================================================
--- z3/jsonserver/trunk/ftests.py (original)
+++ z3/jsonserver/trunk/ftests.py Sun Oct 8 16:08:59 2006
@@ -18,7 +18,7 @@
"""
import zope.interface
import zope.app.folder.folder
-import zope.publisher.interfaces.xmlrpc
+import zope.publisher.interfaces.browser
from zope.app.testing import ztapi, functional, setup
def setUp(test):
@@ -33,19 +33,23 @@
# (OK, we could get it if we want. Maybe later.)
ztapi.provideView(zope.app.folder.folder.IFolder,
- zope.publisher.interfaces.IRequest,
+ zope.publisher.interfaces.browser.IBrowserRequest,
zope.interface,
'folderlist',
None,
)
-
ztapi.provideView(zope.app.folder.folder.IFolder,
- zope.publisher.interfaces.xmlrpc.IXMLRPCRequest,
+ zope.publisher.interfaces.browser.IBrowserRequest,
zope.interface,
'sum',
None,
)
-
+ ztapi.provideView(zope.app.folder.folder.IFolder,
+ zope.publisher.interfaces.browser.IBrowserRequest,
+ zope.interface,
+ 'sum_form.html',
+ None,
+ )
setup.tearDownTestAsModule(test)
def test_suite():
Added: z3/jsonserver/trunk/tests/test_sum_form.pt
==============================================================================
--- (empty file)
+++ z3/jsonserver/trunk/tests/test_sum_form.pt Sun Oct 8 16:08:59 2006
@@ -0,0 +1,14 @@
+<html>
+ <head>
+ <title>This is a form for testing stupid sums</title>
+ </head>
+ <body>
+ <h2>Stupid form test</h2>
+ <p>Enter two values, and submit</p>
+ <form action="sum" method="post">
+ <input type="text" name="a" value=""/>
+ <input type="text" name="b" value=""/>
+ <input type="submit" name="submit" value="OK" />
+ </form>
+ </body>
+</html>
More information about the z3-checkins
mailing list