Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Unix & Linux ServerKDE Sets Ambitious Goals for 2026 and Beyond(23.09.2026 um 22:28 Uhr)
Unix & Linux ServerDSA-6510-1 xdg-dbus-proxy - security update(23.09.2026 um 02:00 Uhr)
Sichere ProgrammierungAPI & API Rest(23.09.2026 um 22:22 Uhr)
Unix & Linux ServerKDE Sets Ambitious Goals for 2026 and Beyond(23.09.2026 um 22:28 Uhr)
Unix & Linux ServerDSA-6510-1 xdg-dbus-proxy - security update(23.09.2026 um 02:00 Uhr)
Sichere ProgrammierungAPI & API Rest(23.09.2026 um 22:22 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

MSW Has a Free API That Makes API Mocking in Tests and Dev Actually Work

Mock Service Worker (MSW) intercepts network requests at the service worker level. No monkey-patching fetch. No test-specific HTTP clients. Your code doesn't know it's being mocked. Setup: Define Handlers import { http,…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Mock Service Worker (MSW) intercepts network requests at the service worker level. No monkey-patching fetch. No test-specific HTTP clients. Your code doesn't know it's being mocked.






Setup: Define Handlers






import { http, HttpResponse } from "msw";

export const handlers = [
// GET request
http.get("/api/products", () => {
return HttpResponse.json([
{ id: 1, title: "Widget", price: 29.99 },
{ id: 2, title: "Gadget", price: 49.99 },
]);
}),

// POST with request body
http.post("/api/products", async ({ request }) => {
const body = await request.json();
return HttpResponse.json({ id: 3, ...body }, { status: 201 });
}),

// Dynamic params
http.get("/api/products/:id", ({ params }) => {
return HttpResponse.json({ id: params.id, title: "Product" });
}),

// Query parameters
http.get("/api/search", ({ request }) => {
const url = new URL(request.url);
const query = url.searchParams.get("q");
return HttpResponse.json({ results: [], query });
}),

// Error responses
http.delete("/api/products/:id", () => {
return new HttpResponse(null, { status: 204 });
}),
];









Testing: Vitest/Jest Integration






import { setupServer } from "msw/node";
import { handlers } from "./handlers";

const server = setupServer(...handlers);

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

test("fetches products", async () => {
const res = await fetch("/api/products");
const data = await res.json();
expect(data).toHaveLength(2);
expect(data[0].title).toBe("Widget");
});

test("handles errors", async () => {
// Override for this test only
server.use(
http.get("/api/products", () => {
return HttpResponse.json({ error: "Server Error" }, { status: 500 });
})
);

const res = await fetch("/api/products");
expect(res.status).toBe(500);
});









Browser: Development Mocking






// src/mocks/browser.ts
import { setupWorker } from "msw/browser";
import { handlers } from "./handlers";

export const worker = setupWorker(...handlers);

// main.tsx
if (process.env.NODE_ENV === "development") {
const { worker } = await import("./mocks/browser");
await worker.start();
}









Network Behaviors






import { http, HttpResponse, delay } from "msw";

// Simulate slow network
http.get("/api/slow", async () => {
await delay(3000);
return HttpResponse.json({ data: "finally" });
});

// Simulate network error
http.get("/api/broken", () => {
return HttpResponse.error();
});

// Streaming response
http.get("/api/stream", () => {
const stream = new ReadableStream({
async start(controller) {
controller.enqueue(new TextEncoder().encode("chunk 1\n"));
await delay(100);
controller.enqueue(new TextEncoder().encode("chunk 2\n"));
controller.close();
},
});
return new HttpResponse(stream);
});









Passthrough: Let Some Requests Through






import { http, passthrough } from "msw";

http.get("/api/analytics/*", () => {
return passthrough(); // Don't mock analytics
});









Testing scraping integrations? My Apify tools + MSW = reliable test suites for data pipelines.



Custom testing solution? Email [email protected]

IR-PLAYBOOK-RCE
HIGH
SOC Incident Playbook: Remote Code Execution (RCE) Defense
1-Click Detection Engineering: Sigma & YARA Rules
SOC Ready
title: Detect Exploitation - MSW Has a Free API That Makes API Mocking in Tests and Dev Actually Work
id: 95545bb0-e331-4afd-8f7f-78da8408dc94
status: experimental
description: Automatisch generierte SIEM-Erkennungsregel basierend auf CTI Intelligence
references:
  - https://tsecurity.de/
author: iShareStuff CTI Automated Detection Engine
date: 2026-09-23
logsource:
  category: network_connection
  product: any
detection:
  selection:
      CommandLine|contains:
        - 'exploit'
  condition: selection
falsepositives:
  - Legitime administrative Zugriffe oder Penetrationstests
level: high
tags:
  - attack.initial_access
rule CTI_Threat_Indicator {
    meta:
        author = "iShareStuff CTI Automated Detection Engine"
        date = "2026-09-23"
        description = "YARA Signature for "
    strings:
        $str = "MSW Has a Free API That Makes " ascii wide
    condition:
        any of them
}
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten MSW Has a Free API That Makes API Mocking in Tests and Dev Actually Work

Thematisch verwandte Begriffe: Free, That, Makes, Mocking · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-90904 | Joomla Extension - joomshaper.com - Broken Access Control (ACL Bypass) i…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel TTP ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick