2017 © Pedro Peláez
 

library yii2-header-environment

Loading environment variables from HTTP Headers (for testing purposes)

image

horat1us/yii2-header-environment

Loading environment variables from HTTP Headers (for testing purposes)

  • Sunday, April 15, 2018
  • by Horat1us
  • Repository
  • 1 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 167 % Grown

The README.md

Yii2 Header Behavior

Latest Stable Version Total Downloads Build Status codecov, (*1)

Purpose of this package is to load environment variables from HTTP headers.
Case: your API receives hash and need to check it by secret key stored in environment. Your test case can not directly use putenv because API and Test API Client is executed in different flows., (*2)

Notice: you should use this package only in test environment!, (*3)

Installation

composer require horat1us/yii2-header-environment --dev # Don't forget to do not use it in production

Usage

Controller:, (*4)

<?php

namespace App\Controllers;

use yii\web;
use Horat1us\HeaderEnvironment;

class SiteController extends web\Controller
{
    public function behaviors()
    {
        $behaviors = []; // Some your production behaviors
        if(YII_ENV_TEST) {
            $behaviors['environment'] = [
                'class' => HeaderEnvironment\Behavior::class,
                'header' => 'Set-Environment', // default
            ];
        } 
    }

    public function actionIndex()
    {
        $request = \Yii::$app->request;

        $salt = $request->post('salt');
        $sign = $request->post('sign');

        $secret = getenv('SECRET');

        \Yii::$app->response->statusCode = md5($salt . $secret) === $sign
            ? 200
            : 400;
    }
}

Test Case:, (*5)

<?php

namespace App\Tests;

class ApiTest {
    public function testSignChecking(\ApiTester $I) {
        $secret = 'persist-secret';
        $salt = mt_rand();
        $sign = md5($salt . $secret);

        $I->haveHttpHeader('Set-Environment', json_encode([
            'SECRET' => $secret,
        ]));
        $I->sendPOST('/site/index', [
            'salt' => $salt,
            'sign' => $sign,
        ]);
        $I->seeResponseCodeIs(200);

        $I->haveHttpHeader('Set-Environment', json_encode([
            'SECRET' => null, // Delete environment
        ]));
        $I->sendPOST('/site/index', [
            'salt' => $salt,
            'sign' => $sign,
        ]);
        $I->seeResponseCodeIs(400);
    }
}

Author

License

MIT, (*6)

The Versions

15/04 2018

1.0.1

1.0.1.0

Loading environment variables from HTTP Headers (for testing purposes)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alexander Letnikow

15/04 2018

dev-master

9999999-dev

Loading environment variables from HTTP Headers (for testing purposes)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alexander Letnikow

15/04 2018

1.0.0

1.0.0.0

Loading environment variables from HTTP Headers (for testing purposes)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alexander Letnikow