Giter Site home page Giter Site logo

apitest's Introduction

Apitest

Easy way to test HTTP APIs with more readable and less verbose code.

With standard library With ApiTest
post_body := map[string]interface{}{
    "hello": "World!",
}

post_json, _ := json.Marshal(post_body)
// TODO: handle post_json error

post_bytes := bytes.NewBuffer(post_json)

request, _ := http.NewRequest("POST",
    "https://httpbin.org/post", post_bytes)
request.Header.Set("X-My-Header", "hello")
// TODO: handle request error

response, _ := http.DefaultClient.Do(request)
// TODO: handle response error

response_body := map[string]interface{}{}

_ = json.NewDecoder(response.Body).
    Decode(&response_body)
// TODO: handle response error

fmt.Println("Check response:", response_body)
			
a := apitest.NewWithBase("https://httpbin.org")

r := a.Request("POST", "/post").
    WithHeader("X-My-Header", "hello").
    WithBodyJson(map[string]interface{}{
        "hello": "World!",
    }).Do()

response_body := r.BodyJson()

fmt.Println("Check response:", response_body)
			

Getting started

my_api := golax.NewApi()

// build `my_api`...

testserver := apitest.New(my_api)

r := testserver.Request("POST", "/users/23/items").
    WithHeader("Content-Type", "application/json").
    WithCookie("sess_id", "123123213213213"),
    WithBodyString(`
        {
            "name": "pencil",
            "description": "Blah blah..."
        }
    `).
    Do()

r.StatusCode // Check this
r.BodyString() // Check this

Sending body JSON

r := testserver.Request("POST", "/users/23/items").
    WithBodyJson(map[string]interface{}{
        "name": "pencil",
        "description": "Blah blah",
    }).
    Do()

Reading body JSON

r := testserver.Request("GET", "/users/23").
    Do()
    
body := r.BodyJson()

Asynchronous request

func Test_Example(t *testing.T) {

	a := golax.NewApi()

	a.Root.Node("users").Method("GET", func(c *golax.Context) {
		fmt.Fprint(c.Response, "John")
	})

	s := apitest.New(a)

	w := &sync.WaitGroup{}

	for i := 0; i < 10; i++ {
		w.Add(1)
		n := i
		go s.Request("GET", "/users").DoAsync(func(r *apitest.Response) {

			if http.StatusOK != r.StatusCode {
				t.Error("Expected status code is 200")
			}

			fmt.Println(r.BodyString(), n)

			w.Done()
		})
	}

	w.Wait()
}

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.