Skip to content

Commit 2b19623

Browse files
committed
AC-746: Malformed request body or parameters cause "Internal Server Error"
Added test coverage for the change (Update error response to 400 related to malformed body/param instead of 500)
1 parent a7149bc commit 2b19623

File tree

1 file changed

+233
-0
lines changed

1 file changed

+233
-0
lines changed
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\Framework\Webapi;
10+
11+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
12+
use Magento\Indexer\Test\Fixture\Indexer as IndexerFixture;
13+
use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture;
14+
use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture;
15+
use Magento\TestFramework\Fixture\DataFixture;
16+
use Magento\TestFramework\Fixture\DataFixtureStorage;
17+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
18+
use Magento\TestFramework\TestCase\WebapiAbstract;
19+
20+
/**
21+
* Test API error processor for malformed requests/bodies.
22+
*/
23+
class ApiErrorProcessorTest extends WebapiAbstract
24+
{
25+
private const RESOURCE_PATH = '/V1/';
26+
27+
/**
28+
* @var DataFixtureStorage
29+
*/
30+
protected $fixtures;
31+
32+
protected function setUp(): void
33+
{
34+
parent::setUp();
35+
36+
/** @var DataFixtureStorage $fixtures */
37+
$this->fixtures = DataFixtureStorageManager::getStorage();
38+
}
39+
40+
/**
41+
* Test that the API returns a 400 error when the request params are malformed.
42+
*
43+
* @param array $requestData
44+
* @param string $endpoint
45+
* @param int $expectedExceptionCode
46+
*
47+
* @dataProvider malformedRequestParamsDataProvider
48+
*/
49+
public function testMalformedRequestParams(array $requestData, string $endpoint, int $expectedExceptionCode)
50+
{
51+
$this->expectException(\Exception::class);
52+
$this->expectExceptionCode($expectedExceptionCode);
53+
54+
$serviceInfo = [
55+
'rest' => [
56+
'resourcePath' => self::RESOURCE_PATH . $endpoint . '?' . http_build_query($requestData),
57+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
58+
]
59+
];
60+
61+
$result = $this->_webApiCall($serviceInfo, $requestData);
62+
}
63+
64+
/**
65+
* Data provider for testMalformedRequestParams
66+
*
67+
* @return array
68+
*/
69+
public static function malformedRequestParamsDataProvider()
70+
{
71+
return [
72+
'empty_filter_groups_field' => [
73+
'requestData' => [
74+
'searchCriteria' => [
75+
'filterGroups' => [
76+
[
77+
'filters' => [
78+
[
79+
'value' => 'string'
80+
]
81+
]
82+
]
83+
]
84+
]
85+
],
86+
'endpoint' => 'eav/attribute-sets/list',
87+
'expectedExceptionCode' => 400,
88+
],
89+
'empty_filter_groups_value' => [
90+
'requestData' => [
91+
'searchCriteria' => [
92+
'filterGroups' => [
93+
[
94+
'filters' => [
95+
[
96+
'field' => 'string'
97+
]
98+
]
99+
]
100+
]
101+
]
102+
],
103+
'endpoint' => 'coupons/search',
104+
'expectedExceptionCode' => 400,
105+
],
106+
'empty_filter_groups_field2' => [
107+
'requestData' => [
108+
'searchCriteria' => [
109+
'filterGroups' => [
110+
[
111+
'filters' => [
112+
[
113+
'value' => 'string'
114+
]
115+
]
116+
]
117+
]
118+
]
119+
],
120+
'endpoint' => 'cmsBlock/search',
121+
'expectedExceptionCode' => 400,
122+
],
123+
'empty_filter_groups_value2' => [
124+
'requestData' => [
125+
'searchCriteria' => [
126+
'filterGroups' => [
127+
[
128+
'filters' => [
129+
[
130+
'field' => 'string'
131+
]
132+
]
133+
]
134+
]
135+
]
136+
],
137+
'endpoint' => 'categories/attributes',
138+
'expectedExceptionCode' => 400,
139+
],
140+
'empty_sort_orders' => [
141+
'requestData' => [
142+
'searchCriteria' => [
143+
'sortOrders' => [
144+
[
145+
'field' => 'string'
146+
]
147+
]
148+
]
149+
],
150+
'endpoint' => 'cmsPage/search',
151+
'expectedExceptionCode' => 400,
152+
]
153+
];
154+
}
155+
156+
/**
157+
* Test that the POST API returns a 400 error when the request body is malformed.
158+
*/
159+
#[
160+
DataFixture(ProductFixture::class, as: 'product'),
161+
DataFixture(IndexerFixture::class, as: 'indexer'),
162+
DataFixture(GuestCartFixture::class, as: 'cart'),
163+
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']),
164+
]
165+
public function testPOSTWithMalformedBody(): void
166+
{
167+
$this->expectException(\Exception::class);
168+
$this->expectExceptionCode(400);
169+
170+
$cart = $this->fixtures->get('cart');
171+
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
172+
$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
173+
->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)
174+
->create();
175+
$quoteIdMask->load($cart->getId(), 'quote_id');
176+
//Use masked cart Id
177+
$cartId = $quoteIdMask->getMaskedId();
178+
179+
$serviceInfo = [
180+
'rest' => [
181+
'resourcePath' => self::RESOURCE_PATH . 'guest-carts/' . $cartId . '/shipping-information',
182+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
183+
],
184+
];
185+
$requestData = [
186+
"addressInformation" => [
187+
"extension_attributes" => [
188+
"discounts" => [
189+
[
190+
"discount_data" => [
191+
"amount" => 0
192+
]
193+
]
194+
]
195+
]
196+
]
197+
];
198+
$this->_webApiCall($serviceInfo, $requestData);
199+
}
200+
201+
/**
202+
* Test that the PUT API returns a 400 error when the request body is malformed.
203+
*/
204+
#[
205+
DataFixture(ProductFixture::class, as: 'product'),
206+
DataFixture(IndexerFixture::class, as: 'indexer'),
207+
]
208+
public function testPUTWithMalformedBody(): void
209+
{
210+
$this->expectException(\Exception::class);
211+
$this->expectExceptionCode(400);
212+
213+
$product = $this->fixtures->get('product');
214+
$sku = $product->getSku();
215+
216+
$serviceInfo = [
217+
'rest' => [
218+
'resourcePath' => self::RESOURCE_PATH . 'products/' . $sku,
219+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
220+
],
221+
];
222+
$requestData = [
223+
"product" => [
224+
"extension_attributes" => [
225+
"stock_item" => [
226+
"show_default_notification_message" => true
227+
]
228+
]
229+
]
230+
];
231+
$this->_webApiCall($serviceInfo, $requestData);
232+
}
233+
}

0 commit comments

Comments
 (0)