Files
context-mod/docs/development.md
FoxxMD bfc7f8a508 docs: Rewrite and improve operator-related documentation
* Break out documentation into more standalone docs and reorganize into an operator folder
* Remove outdated information on adding bot
* Add additional information on docker install
* Make configuration more opinionated for "recommend" approach
* Add docs on database and caching
* Rewrite operator getting started guide to be more concise
2022-04-22 15:24:56 -04:00

8.2 KiB

TODO add more development sections...

Mocking Reddit API

Using MockServer

Installation

https://www.mock-server.com/mock_server/running_mock_server.html

Easiest way is to install the docker container (from here)

Map port 1080:1080 -- acts as both the proxy port and the UI endpoint with the below URL:

http(s)://localhost:1080/mockserver/dashboard

In your operator configuration define a proxy for snoowrap at the top-level:

snoowrap:
  proxy: 'http://localhost:8010'
  #debug: true # optionally set debug to true to make snoowrap requests output to log

Usage

Forwarding Requests (Monitoring Behavior)

This is what will make MockServer act as an actual proxy server. In this state CM will operate normally. In the MockServer UI you will be able to monitor all requests/responses made.

PUT /mockserver/expectation HTTP/1.1
Host: localhost:8010
Content-Type: application/json
Content-Length: 155

{
    "httpRequest": {},
    "priority": 0,
    "httpForward": {
        "host": "oauth.reddit.com",
        "port": 443,
        "scheme": "HTTPS"
    }
}
CURL
curl --location --request PUT 'http://localhost:8010/mockserver/expectation' \
--header 'Content-Type: application/json' \
--data-raw '{
    "httpRequest": {},
    "priority": 0,
    "httpForward": {
        "host": "oauth.reddit.com",
        "port": 443,
        "scheme": "HTTPS"
    }
}'

Mocking Network Issues

MockServer is a bit confusing and regex'ing for specific paths don't work well (for me??)

The lifecycle of a mock call I do:

Create Network Issue Behavior

All Responses return 403

HTTP
PUT /mockserver/expectation HTTP/1.1
Host: localhost:8010
Content-Type: application/json
Content-Length: 1757

{
    "id": "error",
    "httpRequest": {
        "path": ".*"
    },
    "priority": 1,
    "httpResponse": {
        "statusCode": 403,
        "reasonPhrase": "Forbidden",
        "headers": {
            "Connection": [
                "keep-alive"
            ],
            "Content-Type": [
                "application/json; charset=UTF-8"
            ],
            "x-ua-compatible": [
                "IE=edge"
            ],
            "x-frame-options": [
                "SAMEORIGIN"
            ],
            "x-content-type-options": [
                "nosniff"
            ],
            "x-xss-protection": [
                "1; mode=block"
            ],
            "expires": [
                "-1"
            ],
            "cache-control": [
                "private, s-maxage=0, max-age=0, must-revalidate, no-store, max-age=0, must-revalidate"
            ],
            "x-ratelimit-remaining": [
                "575.0"
            ],
            "x-ratelimit-used": [
                "25"
            ],
            "x-ratelimit-reset": [
                "143"
            ],
            "X-Moose": [
                "majestic"
            ],
            "Accept-Ranges": [
                "bytes"
            ],
            "Date": [
                "Wed, 05 Jan 2022 14:37:37 GMT"
            ],
            "Via": [
                "1.1 varnish"
            ],
            "Vary": [
                "accept-encoding"
            ],
            "Strict-Transport-Security": [
                "max-age=15552000; includeSubDomains; preload"
            ],
            "Server": [
                "snooserv"
            ],
            "X-Clacks-Overhead": [
                "GNU Terry Pratchett"
            ]
        }
    }
}
CURL
curl --location --request PUT 'http://localhost:8010/mockserver/expectation' \
--header 'Content-Type: application/json' \
--data-raw '{
    "id": "error",
    "httpRequest": {
        "path": ".*"
    },
    "priority": 1,
    "httpResponse": {
        "statusCode": 403,
        "reasonPhrase": "Forbidden",
        "headers": {
            "Connection": [
                "keep-alive"
            ],
            "Content-Type": [
                "application/json; charset=UTF-8"
            ],
            "x-ua-compatible": [
                "IE=edge"
            ],
            "x-frame-options": [
                "SAMEORIGIN"
            ],
            "x-content-type-options": [
                "nosniff"
            ],
            "x-xss-protection": [
                "1; mode=block"
            ],
            "expires": [
                "-1"
            ],
            "cache-control": [
                "private, s-maxage=0, max-age=0, must-revalidate, no-store, max-age=0, must-revalidate"
            ],
            "x-ratelimit-remaining": [
                "575.0"
            ],
            "x-ratelimit-used": [
                "25"
            ],
            "x-ratelimit-reset": [
                "143"
            ],
            "X-Moose": [
                "majestic"
            ],
            "Accept-Ranges": [
                "bytes"
            ],
            "Date": [
                "Wed, 05 Jan 2022 14:37:37 GMT"
            ],
            "Via": [
                "1.1 varnish"
            ],
            "Vary": [
                "accept-encoding"
            ],
            "Strict-Transport-Security": [
                "max-age=15552000; includeSubDomains; preload"
            ],
            "Server": [
                "snooserv"
            ],
            "X-Clacks-Overhead": [
                "GNU Terry Pratchett"
            ]
        }
    }
}'

All Responses Timeout

HTTP
PUT /mockserver/expectation HTTP/1.1
Host: localhost:8010
Content-Type: application/json
Content-Length: 251

{
    "id": "error",
    "httpRequest": {
        "path": ".*"
    },
    "priority": 1,
    "httpResponse": {
        "body": "should never receive this",
        "delay": {
            "timeUnit": "SECONDS",
            "value": 60
        }
    }
}
CURL
curl --location --request PUT 'http://localhost:8010/mockserver/expectation' \
--header 'Content-Type: application/json' \
--data-raw '{
    "id": "error",
    "httpRequest": {
        "path": ".*"
    },
    "priority": 1,
    "httpResponse": {
        "body": "should never receive this",
        "delay": {
            "timeUnit": "SECONDS",
            "value": 60
        }
    }
}'

All Responses Drop After Delay (Connection Closed by Server)

HTTP
PUT /mockserver/expectation HTTP/1.1
Host: localhost:8010
Content-Type: application/json
Content-Length: 234

{
    "id": "error",
    "httpRequest": {
        "path": ".*"
    },
    "priority": 1,
    "httpError": {
        "dropConnection": true,
        "delay": {
            "timeUnit": "SECONDS",
            "value": 2
        }
    }
}
CURL
curl --location --request PUT 'http://localhost:8010/mockserver/expectation' \
--header 'Content-Type: application/json' \
--data-raw '{
    "id": "error",
    "httpRequest": {
        "path": ".*"
    },
    "priority": 1,
    "httpError": {
        "dropConnection": true,
        "delay": {
            "timeUnit": "SECONDS",
            "value": 2
        }
    }
}'

Clearing Behavior

PUT /mockserver/clear?type=EXPECTATIONS HTTP/1.1
Host: localhost:8010
Content-Type: application/json
Content-Length: 26

{
    "path": "/user/.*"
}
CURL
curl --location --request PUT 'http://localhost:8010/mockserver/clear?type=EXPECTATIONS' \
--header 'Content-Type: application/json' \
--data-raw '{
    "path": "/.*"
}'