Git Product home page Git Product logo

go-rq's Introduction

rq

Go library for making http requests from .http files.

Installation

go get github.com/go-rq/rq

.http File Syntax

### <name of request>
< {% <pre-request
javascipt> %} 

<method> <url>
<header>:<value>
...

<request body>

< {% <post-request 
javascript> %}

Scripts

Scripts can be embedded in the .http request directly or loaded from

### <name of request>
< path/to/script.js

<method> <url>
<header>:<value>
...

<request body>

< path/to/script.js

Scripting API

setEnv(key string, value string)

The runtime environment variables can be modified from either pre or post request script. No return value.

setEnv('host', 'http://localhost:8000');
getEnv(key string)

Returns the value of the environment variable key.

getEnv('host'); // returns 'http://localhost:8000'
assert(condition boolean, message string)

Some assertion which resolves to a boolean value can be made for the condition with a corresponding message. Each assertion is extracted from the scripting runtime and added to the Response object.

assert(response.status === 200, 'response code is 200');
log(message string)

Appends a log message to Request.Logs property allowing the go runtime to access debug information from the scripting runtime.

log(`the response status code was ${response.statusCode}`);
request and response objects

The response object is available in the post-request script and the request object is available in both the pre and post-request scripts. Changes made to these objects are not reflected in the actual request or response, rather they are used for assertions, control flow, and setting up the environment. There are a few exceptions to this rule, such as the skip property on request, see the next section for more information.

request = {
    name: 'Create A User',
    method: 'POST',
    url: 'http://localhost:8000/users',
    headers: {
      'Content-Type': 'application/json',
    }, 
    body: `{
        name: 'r2d2'
    }`,
}
response = {
    status: 'OK',
    statusCode: 200,
    headers: {
        'Content-Type': 'application/json',
    },
    body: `{
        id: '1234',
        name: 'r2d2'
    }`,

    // `json` is the parsed json body that is only available if the response Content-Type
    // header contains `application/json`
    json: {
      id: '1234',
      name: 'r2d2'
    } 
}
Skipping Requests (pre-request script)

You can prevent the request from being made from pre-request scripts being setting the skip property on the request object to true

Examples

### Create a User
< {% 
    setEnv('host', 'http://localhost:8000');
    setEnv('name', 'r2d2');
%}
POST {{host}}/users
Content-Type: application/json

{
    "name": "{{name}}"
}

< {% 
    assertTrue(response.status === 200, 'response code is 200');
%}

Running .http Files from Go Tests

The package treqs, short for Testing Requests, provides functions for running .http files from Go tests.

package test

import (
  "testing"

  "github.com/go-rq/rq/treqs"
)

func TestFoo(t *testing.T) {
    ctx := rq.WithEnvironment(context.Background(), 
    map[string]string{"host": "http://localhost:8080"}) 

    // run all requests in testdata/foo.http
    treqs.RunFile(t, ctx, "testdata/foo.http")
  
    // run all requests in all .http files in the testdata directory
    treqs.RunDir(t, ctx, "testdata")
}

go-rq's People

Contributors

bnprtr avatar dependabot[bot] avatar github-actions[bot] 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.