Prechádzať zdrojové kódy

complete TextTest: add test for ::input with buffer exeeeds max package length

jhdxr 2 rokov pred
rodič
commit
79f59578f8

+ 15 - 0
tests/Pest.php

@@ -24,6 +24,8 @@
 |
 */
 
+use Workerman\Connection\TcpConnection;
+
 expect()->extend('toBeOne', function () {
     return $this->toBe(1);
 });
@@ -43,3 +45,16 @@ function something()
 {
     // ..
 }
+
+function testWithConnectionClose(Closure $closure, string $dataContains = null, $connectionClass = TcpConnection::class): void
+{
+    $tcpConnection = Mockery::spy($connectionClass);
+    $closure($tcpConnection);
+    if ($dataContains) {
+        $tcpConnection->shouldHaveReceived('close', function ($actual) use ($dataContains) {
+            return str_contains($actual, $dataContains);
+        });
+    } else {
+        $tcpConnection->shouldHaveReceived('close');
+    }
+}

+ 4 - 16
tests/Unit/Protocols/HttpTest.php

@@ -20,20 +20,8 @@ it('customizes request class', function () {
 });
 
 it('tests ::input', function () {
-    $testWithConnectionClose = function (Closure $closure, string $dataContains = null) {
-        $tcpConnection = Mockery::spy(TcpConnection::class);
-        $closure($tcpConnection);
-        if ($dataContains) {
-            $tcpConnection->shouldHaveReceived('close', function ($actual) use ($dataContains) {
-                return str_contains($actual, $dataContains);
-            });
-        } else {
-            $tcpConnection->shouldHaveReceived('close');
-        }
-    };
-
     //test 413 payload too large
-    $testWithConnectionClose(function (TcpConnection $tcpConnection) {
+    testWithConnectionClose(function (TcpConnection $tcpConnection) {
         expect(Http::input(str_repeat('jhdxr', 3333), $tcpConnection))
             ->toBe(0);
     }, '413 Payload Too Large');
@@ -47,19 +35,19 @@ it('tests ::input', function () {
         '{"key": "value", "foo": "bar"}';
 
     //unrecognized method
-    $testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
+    testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
         expect(Http::input(str_replace('POST', 'MIAOWU', $buffer), $tcpConnection))
             ->toBe(0);
     }, '400 Bad Request');
 
     //HTTP 1.1 without Host header
-    $testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
+    testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
         expect(Http::input(str_replace("Host: ", 'NotHost: ', $buffer), $tcpConnection))
             ->toBe(0);
     }, '400 Bad Request');
 
     //content-length exceeds connection max package size
-    $testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
+    testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
         $tcpConnection->maxPackageSize = 10;
         expect(Http::input($buffer, $tcpConnection))
             ->toBe(0);

+ 6 - 0
tests/Unit/Protocols/TextTest.php

@@ -7,6 +7,12 @@ test(Text::class, function () {
     $connection = Mockery::mock(ConnectionInterface::class);
 
     //::input
+    //input too long
+    testWithConnectionClose(function ($connection) {
+        $connection->maxPackageSize = 5;
+        expect(Text::input('abcdefgh', $connection))
+            ->toBe(0);
+    });
     //input without "\n"
     expect(Text::input('jhdxr', $connection))
         ->toBe(0);