rest

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2020 License: BSD-2-Clause Imports: 19 Imported by: 0

README

rest

Goes well with rest.vim

Build rest requests for testing. The format is similar to vim-rest-console:

Example file

image

Command line execution

$ go install github.com/taybart/rest/cmd/rest
$ cat <<EOF > post.rest
http://localhost:8080
Content-Type: application/json
POST /
{
  "data": "Yeah!"
}
EOF
$ rest -f post.rest
{ "status": "ok" }

# STDIN
$ rest -i
http://localhost:8080
GET /
# <C-d>
http://localhost:8080/
HTTP/1.1 200 OK
Content-Length: 16
Content-Type: text/plain; charset=utf-8
Date: Fri, 06 Dec 2019 18:55:01 GMT

{"status":"ok"}

---

Programatic parsing

import (
  "fmt"

  "github.com/taybart/rest"
)

func main {
  r := rest.New()
  err := r.Read("./test/post.rest")
  if err != nil {
    panic("HOLY SHIT!")
  }
  success, failed := r.Exec() // Execute dem requests
  fmt.Println(success, failed)
}
System tests!
set HOST real.url.net
set ID 123

https://${HOST}
Content-Type: application/json
POST /user
{ "id": ${ID}, "name": "taybart" }

expect 200

---

delay 3s

https://${HOST}
Content-Type: application/json
GET /user?id=${ID}

expect 200 { "id": ${ID}, "name": "taybart" }

~ » rest -i
https://httpbin.org
GET /status/401
expect 200
Failed requests
Incorrect status code returned 200 != 401
body:
Create other language requests!
import (
  "fmt"

  "github.com/taybart/rest"
)

func main() {
  r := rest.New()
  err := r.Read("./test/post.rest")
  if err != nil {
  fmt.Println(err)
    panic("HOLY SHIT!")
  }
  requests, err := r.SynthesizeRequest("javascript")
  if err != nil {
    fmt.Println(err)
    panic("HOLY SHIT!")
  }
  fmt.Println(requests[0])
}
// output
fetch('http://localhost:8080/post-test', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json', },
  body: JSON.stringify({  "data": "Yeah!"}),
}).then((res) => { if (res.status == 200) { /* woohoo! */ } })
Create full clients
$ rest -f ./test/client.rest -c -o go > client.go
// client.go
package main
import (
  "fmt"
  "io/ioutil"
  "net/http"
  "strings"
)
// Client : my client
type Client struct { }

func (c Client) GetThing() {
  req, err := http.NewRequest("GET", "http://localhost:8080/", nil)

  res, err := http.DefaultClient.Do(req)
  if err != nil {
    fmt.Println(err)
  }
  defer res.Body.Close()
  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
  }
  fmt.Println(string(body))
}


func (c Client) PostThing() {
  req, err := http.NewRequest("POST", "http://localhost:8080/user", strings.NewReader(`{  "user": "taybart"}`))

  res, err := http.DefaultClient.Do(req)
  if err != nil {
    fmt.Println(err)
  }
  defer res.Body.Close()
  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
  }
  fmt.Println(string(body))
}

Server?

I have had uses for having a server that just accepts requests...So I put it in here:

$ rest -s
2267-11-25 18:38:02 [INFO] Running at localhost:8080

I have also (like many) have used python -m http.server. Mine is not as cool but it does stuff:

$ rest -d
2019-11-25 18:40:01 [INFO] Serving $(pwd) at localhost:8080

But ports!!!

Chill...

$ rest -s -p 1234
2267-11-25 18:38:02 [INFO] Running at localhost:1234

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rest

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

Rest : client

func New

func New() *Rest

New : create new client

func (Rest) CheckExpectation

func (r Rest) CheckExpectation(req request, res *http.Response) error

CheckExpectation : ensure request did what is was supposed to

func (Rest) Exec

func (r Rest) Exec() (successful []string, err error)

Exec : do all loaded requests

func (Rest) ExecIndex

func (r Rest) ExecIndex(i int) (result string, err error)

ExecIndex : do specific block in requests

func (Rest) IsRestFile

func (r Rest) IsRestFile(fn string) (bool, error)

IsRestFile : checks if file can be parsed

func (*Rest) NoColor

func (r *Rest) NoColor()

NoColor : change default execution client

func (*Rest) Read

func (r *Rest) Read(fn string) error

Read : read ordered requests from file

func (*Rest) ReadConcurrent

func (r *Rest) ReadConcurrent(fn string) error

ReadConcurrent : read unordered requests from file

func (*Rest) ReadIO

func (r *Rest) ReadIO(buf io.Reader) error

ReadIO : read ordered requests from io reader

func (*Rest) SetClient

func (r *Rest) SetClient(c *http.Client)

SetClient : change default execution client

func (Rest) SynthesizeClient

func (r Rest) SynthesizeClient(lang string) (string, error)

SynthisizeClient : Holy grail

func (Rest) SynthesizeRequests

func (r Rest) SynthesizeRequests(lang string) ([]string, error)

SynthisizeRequests : output request code

Directories

Path Synopsis
cmd
rest command

Jump to

Keyboard shortcuts

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