### Install Mitm.js
Source: https://github.com/moll/node-mitm/blob/master/README.md
Command to install the Mitm.js library using npm.
```bash
npm install mitm
```
--------------------------------
### Test Setup and Teardown with Mitm.js
Source: https://github.com/moll/node-mitm/blob/master/README.md
Enables and disables Mitm.js interception for each test case using `beforeEach` and `afterEach` hooks. This ensures a clean state for every test.
```javascript
beforeEach(function() { this.mitm = Mitm() })
afterEach(function() { this.mitm.disable() })
```
--------------------------------
### Intercept HTTP Requests with Mitm.js - Assert Headers
Source: https://github.com/moll/node-mitm/blob/master/README.md
Shows how to intercept an HTTP request using Mitm.js and assert on request headers. This example verifies the 'Authorization' header of an incoming request.
```javascript
var Mitm = require("mitm")
var mitm = Mitm()
var Http = require("http")
mitm.on("request", function(req, res) {
req.headers.authorization.must.equal("OAuth DEADBEEF")
})
Http.get("http://example.org")
```
--------------------------------
### Bypass Mitm.js Interception for Specific Connections
Source: https://github.com/moll/node-mitm/blob/master/README.md
Demonstrates how to selectively bypass Mitm.js interception for certain connections. This example bypasses interception for connections to a specific host and port, such as a SQL server.
```javascript
var Mitm = require("mitm")
var mitm = Mitm()
mitm.on("connect", function(socket, opts) {
if (opts.host == "sql.example.org" && opts.port == 5432) socket.bypass()
})
```
--------------------------------
### Intercept HTTP Requests with Mitm.js - Mock Response
Source: https://github.com/moll/node-mitm/blob/master/README.md
Illustrates intercepting an HTTP request with Mitm.js and providing a custom server response. This example sets a custom status code and response body.
```javascript
var Mitm = require("mitm")
var mitm = Mitm()
var Http = require("http")
mitm.on("request", function(req, res) {
res.statusCode = 402
res.end("Pay up, sugar!")
})
Http.get("http://example.org", function(res) {
res.setEncoding("utf8")
res.statusCode // => 402
res.on("data", console.log) // => "Pay up, sugar!"
})
```
--------------------------------
### Intercept TCP Connections with Mitm.js
Source: https://github.com/moll/node-mitm/blob/master/README.md
Demonstrates how to intercept a TCP connection using Mitm.js. When a connection is intercepted, a message is sent back to the client. This example uses the `connection` event and interacts with a vanilla `Net.Socket` object.
```javascript
var Mitm = require("mitm")
var mitm = Mitm()
var Net = require("net")
mitm.on("connection", function(socket) { socket.write("Hello back!") })
var socket = Net.connect(22, "example.org")
socket.write("Hello!")
socket.setEncoding("utf8")
socket.on("data", console.log) // => "Hello back!"
```
--------------------------------
### Initialize and Use Mitm.js
Source: https://github.com/moll/node-mitm/blob/master/README.md
Shows the basic usage of Mitm.js: requiring the library and invoking it to create an instance and enable interception.
```javascript
var Mitm = require("mitm")
var mitm = Mitm()
```
--------------------------------
### Mitm.js Events API
Source: https://github.com/moll/node-mitm/blob/master/README.md
Details the events emitted by Mitm.js instances, including 'connect', 'connection', and 'request', along with their respective parameters and descriptions.
```APIDOC
Events
------
All events that Mitm will emit on an instance of itself (see [Using Mitm.js](#using) for examples):
Event | Description
-----------|------------
connect | Emitted when a TCP connection is made.
Given the **client side** `Net.Socket` and `options` from `Net.connect`.
connection | Emitted when a TCP connection is made.
Given the **server side** `Net.Socket` and `options` from `Net.connect`.
request | Emitted when a HTTP/HTTPS request is made.
Given the server side `Http.IncomingMessage` and `Http.ServerResponse`.
```
--------------------------------
### Responding to HTTP Requests with Mitm.js
Source: https://github.com/moll/node-mitm/blob/master/README.md
Allows custom responses to intercepted HTTP requests by setting the status code and ending the response. This mimics the behavior of Node.js HTTP servers.
```javascript
mitm.on("request", function(req, res) {
res.statusCode = 402
res.end("Pay up, sugar!")
})
Http.get("http://example.org", function(res) {
res.statusCode // => 402
res.setEncoding("utf8")
res.on("data", console.log) // => "Pay up, sugar!"
})
```
--------------------------------
### Intercepting HTTP/HTTPS Requests with Mitm.js
Source: https://github.com/moll/node-mitm/blob/master/README.md
Intercepts HTTP and HTTPS requests, providing access to the request and response objects. It listens for the 'request' event on the Mitm instance.
```javascript
mitm.on("request", function(req, res) {
req.headers.authorization.must.equal("OAuth DEADBEEF")
})
Http.get("http://example.org")
```
--------------------------------
### Intercepting TCP Connections with Mitm.js
Source: https://github.com/moll/node-mitm/blob/master/README.md
Intercepts new TCP connections and provides the server-side Net.Socket for custom responses. It listens for the 'connection' event on the Mitm instance.
```javascript
mitm.on("connection", function(socket) { socket.write("Hello back!") })
var socket = Net.connect(22, "example.org")
socket.write("Hello!")
socket.setEncoding("utf8")
socket.on("data", console.log) // => "Hello back!"
```
--------------------------------
### Bypassing Localhost Connections
Source: https://github.com/moll/node-mitm/blob/master/README.md
A common use case for bypassing is to exclude connections to 'localhost' when running integration tests against a local HTTP server, while still intercepting other connections.
```javascript
mitm.on("connect", function(socket, opts) {
if (opts.host == "localhost") socket.bypass()
})
```
--------------------------------
### Disable Mitm.js Interception
Source: https://github.com/moll/node-mitm/blob/master/README.md
Demonstrates how to disable Mitm.js interception after it has been enabled.
```javascript
mitm.disable()
```
--------------------------------
### Bypassing Mitm.js Interception
Source: https://github.com/moll/node-mitm/blob/master/README.md
Allows selective bypassing of Mitm.js interception for specific TCP connections by calling `socket.bypass()` within the 'connect' event handler. This is useful for excluding certain connections from interception.
```javascript
mitm.on("connect", function(socket, opts) {
if (opts.host == "sql.example.org" && opts.port == 5432) socket.bypass()
})
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.