What You'll Learn
- What boundary value analysis is
- How to test data validation thoroughly
- Specific edge cases every API should be tested with
- How to design test data that catches real bugs
Why This Matters
Most API bugs live at the edges. The API works for typical inputs but fails for empty strings, very long strings, negative numbers, dates at midnight, leap years, special characters, and Unicode. Boundary testing systematically probes these edges. Without it, your test suite gives false confidence and bugs slip through to production.
What Is Boundary Value Analysis?
Boundary value analysis (BVA) is the practice of testing values at the edges of allowed ranges, plus values just outside them. The intuition: bugs cluster at boundaries because developers often write >= when they mean >, or forget the empty case.
Example — age field accepting 0–120
| Test value | Boundary | Expected result |
|---|---|---|
| -1 | Just below min | 422 rejected |
| 0 | Min boundary | 201 accepted |
| 1 | Just above min | 201 accepted |
| 119 | Just below max | 201 accepted |
| 120 | Max boundary | 201 accepted |
| 121 | Just above max | 422 rejected |
For Every Field, Test These Cases
String fields
- Empty string
"" - Single character
"a" - Maximum allowed length (e.g., 255 chars)
- One character over max (e.g., 256 chars)
- Only whitespace
" " - Leading/trailing whitespace
" Anita " - Special characters:
!@#$%^&*() - Unicode:
Anita Müller,李明 - Emoji:
Anita 🚀 - HTML:
<script>alert(1)</script> - SQL-like:
'; DROP TABLE users; -- - Null bytes:
"Anita\0Malicious" - Newlines:
"Anita\nRavi"
Numeric fields
- Zero
0 - Negative
-1 - Maximum allowed (e.g., 999999)
- One over max
- Float when integer expected:
3.14 - String when number expected:
"3.14" - Very large:
9999999999999999 - Scientific notation:
1e10 - NaN, Infinity (these are not valid JSON but may slip through)
Date/time fields
- Valid date:
2026-08-23 - Invalid format:
23-08-2026 - February 29 on a non-leap year:
2025-02-29 - February 29 on a leap year:
2024-02-29 - Midnight:
2026-08-23T00:00:00Z - End of day:
2026-08-23T23:59:59Z - Distant past:
0001-01-01 - Distant future:
9999-12-31 - Timezone-aware vs naive:
2026-08-23T10:00:00+05:30 - Unix timestamp 0:
1970-01-01T00:00:00Z
Boolean fields
true(lowercase, valid JSON)falseTrue(Python-style, invalid JSON)TRUE(invalid JSON)1/0(some APIs accept)"true"(string, not boolean)null
Arrays
- Empty array
[] - One element
- Max elements (e.g., 100)
- Over max (101 elements)
- Duplicate elements
[1, 1, 1] - Mixed types
[1, "two", true](usually invalid) - Null element
[1, null, 3]
Email fields
- Valid:
anita@example.com - No @:
anitaexample.com - Multiple @:
anita@example@com - Empty domain:
anita@.com - Spaces:
anita @example.com - Very long (300+ chars)
- Unicode:
anita@münchen.de - Subdomain:
a@b.c.example.com
Designing Test Data — A Practical Approach
For each field, generate test data systematically:
function generateStringTests(fieldName, maxLength) {
return [
{name: `${fieldName} empty`, value: ""},
{name: `${fieldName} single char`, value: "a"},
{name: `${fieldName} max length`, value: "a".repeat(maxLength)},
{name: `${fieldName} over max`, value: "a".repeat(maxLength + 1)},
{name: `${fieldName} whitespace only`, value: " "},
{name: `${fieldName} with unicode`, value: "Anita Müller"},
{name: `${fieldName} with emoji`, value: "Anita 🚀"},
{name: `${fieldName} with HTML`, value: ""},
{name: `${fieldName} with SQL`, value: "'; DROP TABLE users; --"},
];
}
The "What Could Possibly Go Wrong" Test
For each field, ask: what would a malicious or confused user send? Then send it. Examples:
- Extremely long string: 10MB of text. Does the API reject or crash?
- Nested JSON 100 levels deep: Does the parser handle it?
- Duplicate keys in JSON:
{"name": "A", "name": "B"}— which value wins? - Unexpected fields:
{"name": "Anita", "is_admin": true}— does the API silently acceptis_admin? - Numbers as strings:
{"age": "28"}— does the API coerce or reject? - Very precise floats:
{"price": 19.999999999}— does it round correctly?
The "Mass Assignment" Bug
One specific bug to test for: mass assignment. The client sends extra fields the API does not expect, and the API blindly writes them to the database.
POST /users
{
"name": "Anita",
"email": "anita@example.com",
"is_admin": true ← client should not be able to set this
}
If the API does Object.assign(user, requestBody) without filtering, Anita just became an admin. Test that the API rejects or ignores unexpected fields.
Common Mistakes
- Testing only "normal" values. Most bugs live at the edges. Always test boundaries.
- Not testing with special characters. Quotes, angle brackets, ampersands, Unicode — these break many APIs.
- Assuming the client will never send bad data. Clients will. Always validate server-side.
- Forgetting leap years and timezones. Date bugs are subtle and common.
- Not testing with extremely large inputs. A 10MB string should not crash the server.
- Not testing mass assignment. Extra fields should be rejected, not silently persisted.
Practical Exercise (20 minutes)
Pick a POST endpoint. For each field in the request body, write at least 5 boundary/edge case tests. Run each with cURL or Postman. Document any case where the API does not handle the input correctly.
Mini Challenge
Build a "fuzz" test that sends 100 randomly generated requests to an endpoint — random types, random lengths, random characters. Run it and see how many produce unexpected responses. Most APIs will reveal at least one bug under fuzzing.
Key Takeaways
- Boundary value analysis tests at the edges of allowed ranges — where bugs cluster.
- For every field, test: empty, max length, over max, special chars, Unicode, HTML/SQL injection attempts.
- For numbers: zero, negative, max, over max, wrong type.
- For dates: leap years, timezones, midnight, distant past/future.
- Test mass assignment — extra fields should be rejected, not silently stored.
Previously: Lesson 33 covered positive vs negative testing.
Today: You learned to find bugs at the edges and in unexpected inputs.
Next: Lesson 35 covers schema and response validation.
FAQ
Should I test SQL injection in API tests?
Yes, defensively. Send SQL-like strings ('; DROP TABLE users; --) and verify the API stores them as text, not executes them. Modern ORMs handle this, but testing confirms your specific setup is safe. Never test injection against production databases.
How long should test strings be?
Test with: 1 char, max allowed, max + 1, and a very large value (10KB, 100KB, 1MB). The API should accept up to max and reject above. The very-large test verifies the API does not crash under load.
Comments
Comments
Post a Comment