req

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2023 License: MIT Imports: 15 Imported by: 0

README

req

About

Golang API for making http requests using .http files.

Installation

Go Library
go get github.com/bnprtr/req
CLI
go install github.com/bnprtr/req/cmd/req

.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
Examples
### Get a Foo
< {% 
    request.url = request.url+"?foo=bar"; // add a query param
%}
POST http://localhost:8000/foo
Content-Type: application/json

{
    "foo": "bar"
}

< {% 
    assertTrue("response code is 200", response.status === 200);
%}
Executing from the Terminal UI
req
Running .http Files from Go Tests
package test

import (
  "testing"

  "github.com/bnprtr/req/treqs"
)

func TestFoo(t *testing.T) {
    ctx := req.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")
}

Documentation

Index

Constants

View Source
const (
	RequestSeparator = "###"
)

Variables

View Source
var ErrInvalidRequest = errors.New("invalid request")

Functions

func ResetEnvironment

func ResetEnvironment(ctx context.Context)

func WithEnvironment

func WithEnvironment(ctx context.Context, env map[string]string) context.Context

func WithRequestRunner

func WithRequestRunner(ctx context.Context, runner RequestRunner) context.Context

func WithRuntime

func WithRuntime(ctx context.Context, rt *Runtime) context.Context

Types

type Assertion

type Assertion struct {
	Message string `json:"message"`
	Success bool   `json:"success"`
}
type Header struct {
	Key   string
	Value string
}

type Headers

type Headers []Header

type Request

type Request struct {
	// The name of the request
	// Example: Get User
	Name string

	// PreRequestScript is a piece of JavaScript code that executes before the request is sent.
	// Example: var token = req.environment.get("token");
	PreRequestScript string

	// PostRequestScript is a piece of JavaScript code that executes after the request is sent.
	PostRequestScript string

	// The HTTP method used (GET, POST, PUT, DELETE, etc.)
	Method string

	// The URL of the request
	URL string
	// The HTTP body
	// Note: The body is not parsed.
	// Example: {"foo":"bar"}
	// Example: <xml><foo>bar</foo></xml>
	// Example: foo=bar&baz=qux
	Body string

	// The http Headers
	Headers Headers
}

Request is a struct that holds the HTTP request data.

func ParseFromFile

func ParseFromFile(path string) ([]Request, error)

func ParseRequests

func ParseRequests(input string) ([]Request, error)

func (Request) DisplayName

func (r Request) DisplayName() string

func (Request) Do

func (r Request) Do(ctx context.Context) (*Response, error)

func (Request) HttpText

func (r Request) HttpText() string

func (Request) String

func (r Request) String() string

type RequestRunner

type RequestRunner interface {
	Do(req *http.Request) (*http.Response, error)
}

type Response

type Response struct {
	*http.Response
	PreRequestAssertions  []Assertion
	PostRequestAssertions []Assertion
}

func (*Response) PrettyString

func (resp *Response) PrettyString() (string, error)

func (*Response) Raw

func (resp *Response) Raw() *http.Response

func (*Response) String

func (resp *Response) String() string

type Runtime

type Runtime struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis
cmd
req

Jump to

Keyboard shortcuts

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