or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-7/

HTTP Response Header Inspector

Build a tool that fetches JSON data from an HTTP endpoint and validates the response headers before processing the data. The tool should check for specific header values and respond appropriately based on the header information.

Requirements

Your tool should:

  1. Make an HTTP GET request to fetch JSON data
  2. Inspect HTTP response headers when they arrive, before the body is parsed
  3. Check for a specific header called X-API-Version and verify it matches an expected value
  4. If the header is missing or has an unexpected value, abort the request
  5. If the header is valid, process the JSON data as it streams in
  6. Handle success and error cases appropriately

Implementation

@generates

API

/**
 * Fetches JSON data from a URL and validates response headers.
 *
 * @param {string} url - The URL to fetch data from
 * @param {string} expectedVersion - The expected value of the X-API-Version header
 * @param {function} onData - Callback called for each data item in the items array
 * @param {function} onComplete - Callback called when request completes successfully
 * @param {function} onError - Callback called if headers are invalid or request fails
 * @returns {object} The request object that can be used to abort the request
 */
function fetchWithHeaderValidation(url, expectedVersion, onData, onComplete, onError);

module.exports = { fetchWithHeaderValidation };

Test Cases

  • When X-API-Version header matches expected value "v2.0", data items should be processed @test
  • When X-API-Version header does not match expected value, the error callback should be invoked with header mismatch message @test
  • When X-API-Version header is missing, the error callback should be invoked @test
  • When request completes successfully with valid headers, the completion callback should be invoked @test

Dependencies { .dependencies }

oboe { .dependency }

Provides streaming JSON parsing with request lifecycle event handling.

@satisfied-by