情境
寫 Feature 測試時有時候需要偽造 $_SERVER 變數,例如改 REMOTE_ADDR 來測試 IP 相關的邏輯。
做法
Laravel 的測試方法本身就支援傳入 server 變數。
GET 請求
第二個參數就是 server 變數:
1
2
3
4
5
6
7
8
| $this->get('/api/path', [
'REMOTE_ADDR' => '10.1.0.1'
]);
// JSON 版
$this->getJson('/api/path', [
'REMOTE_ADDR' => '10.1.0.1'
]);
|
POST 請求
第三個參數是 server 變數(第二個是 request body):
1
2
3
4
5
6
7
| $this->post('/api/path', ['foo' => 'bar'], [
'REMOTE_ADDR' => '10.1.0.1'
]);
$this->postJson('/api/path', ['foo' => 'bar'], [
'REMOTE_ADDR' => '10.1.0.1'
]);
|
整個測試共用
不想每個 request 都帶的話,用 withServerVariables() 設一次就好:
1
| $this->withServerVariables(['REMOTE_ADDR' => '10.1.0.1']);
|
之後同一個測試方法裡的所有請求都會套用。