Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit 911c017

Browse files
authored
Merge pull request doctrine#56 from jeqq/multipart-requests-with-content-size
Multipart requests with content size
2 parents 3b403a0 + 2f83077 commit 911c017

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

lib/Doctrine/CouchDB/HTTP/HTTPException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static function fromResponse( $path, Response $response )
6666
}
6767

6868
return new self(
69-
"HTTP Error with status " . $response->status . " occoured while "
69+
"HTTP Error with status " . $response->status . " occurred while "
7070
. "requesting " . $path . ". Error: " . $response->body['error']
7171
. " " . $response->body['reason'],
7272
$response->status );

lib/Doctrine/CouchDB/HTTP/MultipartParserAndSender.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ protected function parseAndSend($targetPath)
178178
{
179179
// Main response boundary of the multipart stream.
180180
$mainBoundary = trim($this->getNextLineFromSourceConnection());
181+
// If on the first line we have the size, then main boundary should be
182+
// on the second line.
183+
if (is_numeric($mainBoundary)) {
184+
$mainBoundary = trim($this->getNextLineFromSourceConnection());
185+
}
181186

182187
// Docs that don't have attachment.
183188
// These should be posted using Bulk upload.
@@ -194,7 +199,6 @@ protected function parseAndSend($targetPath)
194199

195200
} elseif (strpos($line, 'Content-Type') !== false) {
196201

197-
198202
list($header, $value) = explode(':', $line);
199203
$header = trim($header);
200204
$value = trim($value);

tests/Doctrine/Tests/CouchDB/Functional/CouchDBClientTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function testDropMultipleTimesSkips()
7474
public function testCreateDuplicateDatabaseThrowsException()
7575
{
7676
$this->couchClient->createDatabase($this->getTestDatabase());
77-
$this->setExpectedException('Doctrine\CouchDB\HTTP\HTTPException', 'HTTP Error with status 412 occoured while requesting /'.$this->getTestDatabase().'. Error: file_exists The database could not be created, the file already exists.');
77+
$this->setExpectedException('Doctrine\CouchDB\HTTP\HTTPException', 'HTTP Error with status 412 occurred while requesting /'.$this->getTestDatabase().'. Error: file_exists The database could not be created, the file already exists.');
7878
$this->couchClient->createDatabase($this->getTestDatabase());
7979
}
8080

@@ -87,7 +87,7 @@ public function testGetDatabaseInfo()
8787
$this->assertEquals($this->getTestDatabase(), $data['db_name']);
8888

8989
$notExistedDb = 'not_existed_db';
90-
$this->setExpectedException('Doctrine\CouchDB\HTTP\HTTPException','HTTP Error with status 404 occoured while requesting /'.$notExistedDb.'. Error: not_found no_db_file');
90+
$this->setExpectedException('Doctrine\CouchDB\HTTP\HTTPException','HTTP Error with status 404 occurred while requesting /'.$notExistedDb.'. Error: not_found no_db_file');
9191
$this->couchClient->getDatabaseInfo($notExistedDb);
9292
}
9393

@@ -670,7 +670,7 @@ public function test404WhenQueryAndNoDesignDocument()
670670

671671
$this->setExpectedException(
672672
'Doctrine\CouchDB\HTTP\HTTPException',
673-
'HTTP Error with status 404 occoured while requesting /doctrine_test_database/_design/foo/_view/not-found?. Error: not_found missing'
673+
'HTTP Error with status 404 occurred while requesting /doctrine_test_database/_design/foo/_view/not-found?. Error: not_found missing'
674674
);
675675

676676
$query->execute();

tests/Doctrine/Tests/CouchDB/Functional/HTTP/MultipartParserAndSenderTest.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,60 @@ public function testRequestSuccessWithAttachments()
319319
$client->createDatabase($this->getTestDatabase());
320320
$client->deleteDatabase($copyDb);
321321
}
322-
}
322+
323+
/**
324+
* Test multipart request with body size in the request body.
325+
*/
326+
public function testMultipartRequestWithSize()
327+
{
328+
$this->streamClientMock->expects($this->once())
329+
->method('getStreamHeaders')
330+
->willReturn(array('status' => 200));
331+
$docs = array(
332+
'{"_id": "' . $this->docId . '","_rev": "1-abc","foo":"bar"}',
333+
'{"_id": "' . $this->docId . '","_rev": "1-abcd","foo":"baz"}',
334+
'{"_id": "' . $this->docId . '","_rev": "1-abcde","foo":"baz"}',
335+
);
336+
$string = <<<EOT
337+
--7b1596fc4940bc1be725ad67f11ec1c4
338+
Content-Type: application/json
339+
340+
$docs[0]
341+
--7b1596fc4940bc1be725ad67f11ec1c4
342+
Content-Type: application/json
343+
344+
$docs[1]
345+
--7b1596fc4940bc1be725ad67f11ec1c4
346+
Content-Type: application/json
347+
348+
$docs[2]
349+
--7b1596fc4940bc1be725ad67f11ec1c4
350+
EOT;
351+
$size = strlen($string);
352+
// Add the size.
353+
$string = <<<EOT
354+
$size
355+
$string
356+
EOT;
357+
358+
$stream = fopen('data://text/plain,' . $string,'r');
359+
$this->streamClientMock->expects($this->once())
360+
->method('getConnection')
361+
->willReturn($stream);
362+
363+
$response = $this->parserAndSender->request(
364+
$this->sourceMethod,
365+
$this->sourcePath,
366+
$this->targetPath,
367+
null,
368+
$this->sourceHeaders
369+
);
370+
// The returned response should have the JSON docs.
371+
$this->AssertEquals(2, count($response));
372+
$this->AssertEquals(3, count($response[0]));
373+
$this->AssertEquals($docs[0], $response[0][0]);
374+
$this->AssertEquals($docs[1], $response[0][1]);
375+
$this->AssertEquals($docs[2], $response[0][2]);
376+
}
377+
378+
}

0 commit comments

Comments
 (0)