Skip to content

Commit

Permalink
Merge pull request #280 from bowphp/fix-http-duplicate-header
Browse files Browse the repository at this point in the history
Fixes the processing request data from php input
  • Loading branch information
papac authored Nov 9, 2023
2 parents 1e8bfd0 + 99ec54b commit 672b56b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ public function __construct(Request $request, Response $response)

$this->capsule = Capsule::getInstance();

$this->capsule->instance('request', $request);
$this->capsule->instance('response', $response);
$this->capsule->instance('request', $request);
$this->capsule->instance('app', $this);

$this->request->capture();
parent::__construct($request->method(), $request->get('_method'));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Client/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private function addFields(array $data): void
if (count($data) == 0) {
return;
}

if ($this->accept_json) {
$payload = json_encode($data);
} else {
Expand Down
21 changes: 20 additions & 1 deletion src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,24 @@ class Request
*/
private string $id;

/**
* Define the request captured
*
* @var bool
*/
private bool $capture = false;

/**
* Request constructor
*
* @return mixed
*/
private function __construct()
public function capture()
{
if ($this->capture) {
return;
}

$data = [];
$this->id = "req_" . sha1(uniqid() . time());

Expand Down Expand Up @@ -76,6 +87,8 @@ private function __construct()

$this->input[$key] = $value;
}

$this->capture = true;
}

/**
Expand Down Expand Up @@ -348,6 +361,12 @@ public function isAjax(): bool
return true;
}

$content_type = $this->getHeader("content-type");

if ($content_type && str_contains($content_type, "application/json")) {
return true;
}

return false;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Application/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function test_instance_of_application()
$request = Mockery::mock(Request::class);

$request->allows()->method()->andReturns("GET");
$request->allows()->capture()->andReturns(null);
$request->allows()->get("_method")->andReturns("");

$app = Application::make($request, $response);
Expand All @@ -45,6 +46,7 @@ public function test_one_time_application_boot()
$request = Mockery::mock(Request::class);

$request->allows()->method()->andReturns("GET");
$request->allows()->capture()->andReturns(null);
$request->allows()->get("_method")->andReturns("");

$app = Application::make($request, $response);
Expand All @@ -68,6 +70,7 @@ public function test_send_application_with_404_status()

// Request mock method
$request->allows()->method()->andReturns("GET");
$request->allows()->capture()->andReturns(null);
$request->allows()->path()->andReturns("/");
$request->allows()->get("_method")->andReturns("");

Expand Down Expand Up @@ -96,6 +99,7 @@ public function test_send_application_with_matched_route()

// Request mock method
$request->allows()->method()->andReturns("GET");
$request->allows()->capture()->andReturns(null);
$request->allows()->path()->andReturns("/");
$request->allows()->get("_method")->andReturns("");

Expand Down Expand Up @@ -128,6 +132,7 @@ public function test_send_application_with_no_matched_route()

// Request mock method
$request->allows()->method()->andReturns("GET");
$request->allows()->capture()->andReturns(null);
$request->allows()->path()->andReturns("/name");
$request->allows()->get("_method")->andReturns("");

Expand Down
1 change: 0 additions & 1 deletion tests/Queue/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public function test_instance_of_adapter($connection)
} elseif ($connection == "sync") {
$this->assertInstanceOf(SyncAdapter::class, $adapter);
}

}

/**
Expand Down

0 comments on commit 672b56b

Please sign in to comment.