Giter Site home page Giter Site logo

gpiechnik2 / xk6-proxy Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 40 KB

k6 extension to define a separate (independent) proxy from the HTTP_PROXY variables for HTTP requests. Additionally, the extension allows to display and re-set a new proxy in tests. Implemented using the xk6 system.

License: Apache License 2.0

Go 100.00%
xk6 extension k6 performance proxy tests xk6-extension

xk6-proxy's Introduction

xk6-proxy

k6 extension to define a separate (independent) proxy from the HTTP_PROXY variables for HTTP requests. Additionally, the extension allows to display and re-set a new proxy in tests. Implemented using the xk6 system.

Build

xk6 build --with github.com/gpiechnik2/xk6-proxy@latest

Example

On a single request

To use the proxy on http.Request objects from the k6 repository you would have to rebuild the entire class. Since this would take too much time, we use requests from the library, which under the k6 implementation is also used. We do not return the same Response object, but instead a string with the typical response for the http format.

import proxy from 'k6/x/proxy';


export default function () {
    const proxyRes = proxy.request('GET', 'https://k6.io', "http://0.0.0.0:8080", {
        headers: [
            {
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0',
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
            }
        ]
    });

    check(proxyRes, {
        'response code was 200': (proxyRes) => proxyRes.includes("200 OK") == true
    });
}

On global variables

[WARNING] Remember that using global variables affects ALL runs. The example at the bottom should not be used in performing actual tests. Use only for debugging or on a single thread.

import proxy from 'k6/x/proxy';


export default function () {
    let currentEnvHTTP = proxy.getCurrentEnvHTTP() // 0.0.0.0:9000
    let currentEnvHTTPS = proxy.getCurrentEnvHTTPS() // 0.0.0.0:6000
    
    proxy.setEnvHTTP('0.0.0.0:14000') // set a new HTTP_PROXY environment variable
    proxy.setEnvHTTPS('0.0.0.0:10000') // set a new HTTPS_PROXY environment variable

    currentEnvHTTP = proxy.getCurrentEnvHTTP() // 0.0.0.0:14000
    currentEnvHTTPS = proxy.getCurrentEnvHTTPS() // 0.0.0.0:10000
}

Run sample script

./k6 run ./script.js

xk6-proxy's People

Contributors

gpiechnik2 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.