proxy

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Proxy

type Proxy struct{}

Proxy is the k6 extension

func (*Proxy) GetCurrentEnvHTTP

func (*Proxy) GetCurrentEnvHTTP(test string) string

Get the current https proxy in use

func (*Proxy) GetCurrentEnvHTTPS

func (*Proxy) GetCurrentEnvHTTPS(test string) string

Get the current http proxy in use

func (*Proxy) Request

func (*Proxy) Request(method string, targetUrl string, proxyUrl string, options options) string

submit a request with the indicated method, url, proxy url and optional options

func (*Proxy) SetEnvHTTP

func (*Proxy) SetEnvHTTP(proxy string)

Set new http proxy

func (*Proxy) SetEnvHTTPS

func (*Proxy) SetEnvHTTPS(proxy string)

Set new https proxy

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL