]> gitweb.fluxo.info Git - semanticscuttle.git/commitdiff
send 400 status code if a parameter is missing
authorcweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>
Wed, 29 Sep 2010 20:55:14 +0000 (20:55 +0000)
committercweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>
Wed, 29 Sep 2010 20:55:14 +0000 (20:55 +0000)
git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@775 b3834d28-1941-0410-a4f8-b48e95affb8f

tests/Api/PostsAddTest.php
www/api/posts_add.php

index 0769bddd01616df80c5064caa5e2d0d609ba5b4a..dea002ecdb1059162f2319b855b280a412248477 100644 (file)
@@ -199,6 +199,113 @@ TXT;
         );
     }
 
+    /**
+     * Verify that the URL and description/title are enough parameters
+     * to add a bookmark.
+     */
+    public function testUrlDescEnough()
+    {
+        $this->bs->deleteAll();
+
+        list($req, $uId) = $this->getAuthRequest();
+        $req->setMethod(HTTP_Request2::METHOD_POST);
+        $req->addPostParameter('url', 'http://example.org/tag2');
+        $req->addPostParameter('description', 'foo bar');
+        $res = $req->send();
+
+        //all should be well
+        $this->assertEquals(200, $res->getStatus());
+        //verify MIME content type
+        $this->assertEquals(
+            'text/xml; charset=utf-8',
+            $res->getHeader('content-type')
+        );
+
+        //verify xml
+        $this->assertTag(
+            array(
+                'tag'        => 'result',
+                'attributes' => array('code' => 'done')
+            ),
+            $res->getBody(),
+            null, false
+        );
+
+        //user has 1 bookmark now
+        $data = $this->bs->getBookmarks(0, null, $uId);
+        $this->assertEquals(1, $data['total']);
+    }
+
+    /**
+     * Verify that the URL is required
+     */
+    public function testUrlRequired()
+    {
+        $this->bs->deleteAll();
+
+        list($req, $uId) = $this->getAuthRequest();
+        $req->setMethod(HTTP_Request2::METHOD_POST);
+        //$req->addPostParameter('url', 'http://example.org/tag2');
+        $req->addPostParameter('description', 'foo bar');
+        $res = $req->send();
+
+        //all should be well
+        $this->assertEquals(400, $res->getStatus());
+        //verify MIME content type
+        $this->assertEquals(
+            'text/xml; charset=utf-8',
+            $res->getHeader('content-type')
+        );
+
+        //verify xml
+        $this->assertTag(
+            array(
+                'tag'        => 'result',
+                'attributes' => array('code' => 'URL missing')
+            ),
+            $res->getBody(),
+            null, false
+        );
+
+        //user still has 0 bookmarks
+        $data = $this->bs->getBookmarks(0, null, $uId);
+        $this->assertEquals(0, $data['total']);
+    }
+
+    /**
+     * Verify that the description/title is required
+     */
+    public function testDescriptionRequired()
+    {
+        $this->bs->deleteAll();
+
+        list($req, $uId) = $this->getAuthRequest();
+        $req->setMethod(HTTP_Request2::METHOD_POST);
+        $req->addPostParameter('url', 'http://example.org/tag2');
+        $res = $req->send();
+
+        //all should be well
+        $this->assertEquals(400, $res->getStatus());
+        //verify MIME content type
+        $this->assertEquals(
+            'text/xml; charset=utf-8',
+            $res->getHeader('content-type')
+        );
+
+        //verify xml
+        $this->assertTag(
+            array(
+                'tag'        => 'result',
+                'attributes' => array('code' => 'Description missing')
+            ),
+            $res->getBody(),
+            null, false
+        );
+
+        //user still has 0 bookmarks
+        $data = $this->bs->getBookmarks(0, null, $uId);
+        $this->assertEquals(0, $data['total']);
+    }
 }
 
 if (PHPUnit_MAIN_METHOD == 'Api_PostsAddTest::main') {
index b392a8015f9068559c51e2d907491023c81028c5..8b63a169a3257f4388bc4c201002bbbbc618e312 100644 (file)
@@ -73,17 +73,25 @@ if (isset($_REQUEST['shared']) && (trim($_REQUEST['shared']) == 'no')) {
 }
 
 // Error out if there's no address or description
-if (is_null($url) || is_null($description)) {
-    $added = false;
+if (is_null($url)) {
+    header('HTTP/1.0 400 Bad Request');
+    $msg = 'URL missing';
+} else if (is_null($description)) {
+    header('HTTP/1.0 400 Bad Request');
+    $msg = 'Description missing';
 } else {
-// We're good with info; now insert it!
-    if ($bookmarkservice->bookmarkExists($url, $userservice->getCurrentUserId()))
-        $added = false;
-    else
-        $added = $bookmarkservice->addBookmark($url, $description, $extended, '', $status, $tags, null, $dt, true);
+    // We're good with info; now insert it!
+    if ($bookmarkservice->bookmarkExists($url, $userservice->getCurrentUserId())) {
+        $msg = 'something went wrong';
+    } else {
+        $added = $bookmarkservice->addBookmark(
+            $url, $description, $extended, '', $status, $tags, null, $dt, true
+        );
+        $msg = 'done';
+    }
 }
 
 // Set up the XML file and output the result.
-echo '<?xml version="1.0" standalone="yes" ?'.">\r\n";
-echo '<result code="'. ($added ? 'done' : 'something went wrong') .'" />';
+echo '<?xml version="1.0" standalone="yes" ?' . ">\r\n";
+echo '<result code="' . $msg .'" />';
 ?>
\ No newline at end of file