Skip to content

Commit

Permalink
Merge pull request #20 from allejo/feature/vcr-1.5-support
Browse files Browse the repository at this point in the history
  • Loading branch information
allejo authored Jul 29, 2020
2 parents 6a8c607 + 7f2e8b4 commit 9c2ab0e
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 167 deletions.
4 changes: 0 additions & 4 deletions .github/FUNDING.yml

This file was deleted.

65 changes: 65 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Unit Tests

on:
push: ~
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '0 0 * * 1'

jobs:
tests:
name: PHP ${{ matrix.php-versions }} on ${{ matrix.os }} w/ ${{ matrix.dependencies }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
php-versions: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4']
os: [ubuntu-latest]
dependencies: ['install']

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up PHP ${{ matrix.php-versions }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: curl, soap
coverage: xdebug

- name: Setup Problem Matchers for PHP
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"

- name: Setup Problem Matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Validate composer.json and composer.lock
run: composer validate

- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer ${{ matrix.dependencies }} --prefer-dist --no-progress --no-suggest

- name: Show Installed Dependencies
run: composer show

- name: Run test suite
run: composer test

- name: Push to Scrutinizer
if: matrix.php-versions == '7.4' && runner.os == 'Linux' && matrix.dependencies == 'install'
run: |
composer coverage
wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover coverage.clover
49 changes: 0 additions & 49 deletions .travis.yml

This file was deleted.

27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
# php-vcr-sanitizer

[![Packagist](https://img.shields.io/packagist/v/allejo/php-vcr-sanitizer.svg)](https://packagist.org/packages/allejo/php-vcr-sanitizer)
[![Build Status](https://travis-ci.org/allejo/php-vcr-sanitizer.svg?branch=master)](https://travis-ci.org/allejo/php-vcr-sanitizer)
[![php-vcr v1.4+ or v1.5+](https://img.shields.io/badge/php--vcr-1.4%20%7C%201.5-blue)](https://github.com/php-vcr/php-vcr)
![Unit Tests](https://github.com/allejo/php-vcr-sanitizer/workflows/Unit%20Tests/badge.svg)
[![GitHub license](https://img.shields.io/github/license/allejo/php-vcr-sanitizer.svg)](https://github.com/allejo/php-vcr-sanitizer/blob/master/LICENSE.md)


[php-vcr](https://php-vcr.github.io/) is a tool for recording and replaying outgoing requests, however it has had ["Privacy aware" marked as "soon"](https://php-vcr.github.io/#page-nav-Features) for quite some time now. Whenever I test my APIs, there will often be some sensitive information such as keys or passwords in the recordings. Up until now, I've had a separate script to always remove sensitive data before getting checked into version control.

I got tired of having to always sanitize the data, so this is a quick and dirty solution until php-vcr officially supports "private" recordings.

[TOC levels=4]: # "## Table of Contents"
[TOC levels=2-4]: # "## Table of Contents"

## Table of Contents
- [Installation](#installation)
- [Usage](#usage)
- [Configuration](#configuration)
- [Sanitizing Requests](#sanitizing-requests)
- [Sanitizing Responses](#sanitizing-responses)
- [Disabling the Sanitizer](#disabling-the-sanitizer)
- [Configuration](#configuration)
- [Sanitizing Requests](#sanitizing-requests)
- [Sanitizing Responses](#sanitizing-responses)
- [Disabling the Sanitizer](#disabling-the-sanitizer)
- [How Sanitizing Works](#how-sanitizing-works)
- [Hostnames](#hostnames)
- [Headers](#headers)
- [URL Parameters](#url-parameters)
- [Body Content](#body-content)
- [Hostnames](#hostnames)
- [Headers](#headers)
- [URL Parameters](#url-parameters)
- [Body Content](#body-content)
- [Post Field Content](#post-field-content)
- [License](#license)


Expand Down Expand Up @@ -78,15 +79,15 @@ This library allows your sanitize both the Request and Response sections of your

- `request.ignoreHostname` - When set to true, the hostname in URLs inside of the Request will be replaced with `[]` in the `url` field and the `Host` in the headers will be set to null.
- `request.ignoreQueryFields` - Define which GET parameters in your URL to completely strip out of your recordings.
- `request.ignoreHeaders` - Define the headers in your recording that will automatically be set to null in your recordings. A wildcard may be used to strip all headers from the request.
- `request.ignoreHeaders` - Define the headers in your recording that will automatically be set to null in your recordings. Using an asterisk (i.e. `*`) in the array can be used to strip all headers from the request.
- `request.bodyScrubbers` - An array of callbacks that will have the request body available as a string. Each callback **must** return the modified body. The callbacks are called consecutively in the order they appear in this array and the value from one callback propagates to the next.
- `request.postFieldScrubbers` - An array of callbacks that will have the request post fields available as an array. Each callback **must** return the modified post fields array. The callbacks are called consecutively in the order they appear in this array and the value from one callback propagates to the next.

#### Sanitizing Responses

The php-vcr library does not officially support modifying its responses so this library uses reflection to modify the contents of responses. While this feature is officially supported by *this* project, bear with us if this feature were to break due to the php-vcr changing its internals.

- `response.ignoreHeaders` - The same as `request.ignoreHeaders` but for your response body instead. A wildcard may be used to strip all headers from the response.
- `response.ignoreHeaders` - The same as `request.ignoreHeaders` but for your response body instead.
- `response.bodyScrubbers` - The same as `request.bodyScrubbers` but for your response body instead.

### Disabling the Sanitizer
Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"description": "A set of php-vcr utilities to sanitize API requests",
"type": "library",
"require": {
"php": ">=5.4",
"php-vcr/php-vcr": "^1.4",
"symfony/event-dispatcher": "^2.4|^3.0|^4.0"
"symfony/event-dispatcher": "^2.4|^3.0|^4.0|^5.0"
},
"require-dev": {
"ext-curl": "*",
"donatj/mock-webserver": "^2.1",
"mikey179/vfsstream": "^1.6",
"php-curl-class/php-curl-class": "^8.0",
Expand Down Expand Up @@ -34,8 +36,8 @@
"sort-packages": true
},
"scripts": {
"test": "phpunit",
"test-coverage": "phpunit --coverage-clover coverage.clover",
"fix": "php-cs-fixer fix"
"fix": "php-cs-fixer fix",
"coverage": "phpunit --coverage-clover coverage.clover",
"test": "phpunit"
}
}
57 changes: 57 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,98 @@ abstract class Config
{
private static $options = array();

/** @var bool|null */
private static $ignoreAllReqHeaders;

/** @var bool|null */
private static $ignoreAllResHeaders;

/**
* @return void
*/
public static function configureOptions(array $options)
{
self::$options = array_replace_recursive(self::defaultConfig(), $options);

self::$ignoreAllReqHeaders = in_array('*', self::getReqIgnoredHeaders(), true);
self::$ignoreAllResHeaders = in_array('*', self::getResIgnoredHeaders(), true);

if (self::$ignoreAllReqHeaders) {
self::$options['request']['ignoreHeaders'] = array('*');
}

if (self::$ignoreAllResHeaders) {
self::$options['response']['ignoreHeaders'] = array('*');
}
}

/**
* @return bool
*/
public static function ignoreReqHostname()
{
return self::$options['request']['ignoreHostname'];
}

/**
* @return bool
*/
public static function ignoreAllReqHeaders()
{
return self::$ignoreAllReqHeaders === true;
}

/**
* @return bool
*/
public static function ignoreAllResHeaders()
{
return self::$ignoreAllResHeaders === true;
}

/**
* @return string[]
*/
public static function getReqIgnoredQueryFields()
{
return self::$options['request']['ignoreQueryFields'];
}

/**
* @return string[]
*/
public static function getReqIgnoredHeaders()
{
return self::$options['request']['ignoreHeaders'];
}

/**
* @return array<callable(string): string>
*/
public static function getReqBodyScrubbers()
{
return self::$options['request']['bodyScrubbers'];
}

/**
* @return array<callable(array<string, mixed>): string>
*/
public static function getReqPostFieldScrubbers()
{
return self::$options['request']['postFieldScrubbers'];
}

/**
* @return string[]
*/
public static function getResIgnoredHeaders()
{
return self::$options['response']['ignoreHeaders'];
}

/**
* @return array<callable(string): string>
*/
public static function getResBodyScrubbers()
{
return self::$options['response']['bodyScrubbers'];
Expand Down
Loading

0 comments on commit 9c2ab0e

Please sign in to comment.