Your development workspace
This app is private. Sign in with your authorized Google account.
When an HTTP mock deliberately returns malformed JSON, construct a raw response with the intended JSON Content-Type rather than using a text-specific response helper and overriding its media type.
| 1 | HttpResponse.text('{', { |
| 2 | headers: { 'Content-Type': 'application/json' }, |
| 3 | }) |
Explanation (EN)
The text-specific helper and the declared JSON media type communicate different payload formats, obscuring the intentional malformed-JSON fixture.
Objašnjenje (HR)
Pomoćna metoda za tekst i deklarirani JSON tip sadržaja opisuju različite formate pa otežavaju razumijevanje namjerno neispravnog JSON odgovora.
| 1 | new HttpResponse('{', { |
| 2 | headers: { 'Content-Type': 'application/json' }, |
| 3 | }) |
Explanation (EN)
A raw response preserves the deliberately invalid bytes and explicitly declares the format the consumer is expected to parse.
Objašnjenje (HR)
Izravno konstruiran odgovor čuva namjerno neispravne bajtove i eksplicitno navodi format koji potrošač treba parsirati.
Apply to test fixtures that exercise JSON parse failures or similar deliberate wire-format errors. Prefer HttpResponse.json for valid JSON. Passing a string to HttpResponse.json serializes a valid JSON string and cannot model malformed JSON bytes. This is a clarity rule, not a claim that an explicit Content-Type override is ignored. Keep the raw body and intended headers required by the scenario.
Primjenjuje se na testne odgovore koji provjeravaju pogreške parsiranja JSON-a ili slične namjerne greške u formatu. Za ispravan JSON koristi HttpResponse.json. Predaja stringa u HttpResponse.json serijalizira valjani JSON string i ne predstavlja neispravne JSON bajtove. Pravilo poboljšava jasnoću i ne tvrdi da se eksplicitni Content-Type zanemaruje. Sačuvaj tijelo i zaglavlja koja scenarij zahtijeva.
Use format-specific helpers when they accurately describe the response. Adjusting a charset or another parameter within the same media type does not create this mismatch. Empty-response fixtures are a separate concern because malformed-JSON tests require the actual invalid body.
Koristi pomoćne metode za određeni format kada točno opisuju odgovor. Promjena skupa znakova ili drugog parametra unutar istog tipa sadržaja nije ova nepodudarnost. Prazni testni odgovori zasebna su tema jer test neispravnog JSON-a treba stvarno neispravno tijelo.
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).