GithubHelp home page GithubHelp logo

xv-tom-l / go-httpbin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ahmetb/go-httpbin

0.0 0.0 0.0 38 KB

http://httpbin.org endpoints for your Go tests –

Home Page: https://godoc.org/github.com/ahmetalpbalkan/go-httpbin

License: Apache License 2.0

Go 100.00%

go-httpbin's Introduction

go-httpbin

Read GoDoc Build Status

A Go handler that lets you test your HTTP client, retry logic, streaming behavior, timeouts etc. with the endpoints of httpbin.org locally in a net/http/httptest.Server.

This way, you can write tests without relying on an external dependency like httpbin.org.

Endpoints

  • /ip Returns Origin IP.
  • /user-agent Returns user-agent.
  • /headers Returns headers.
  • /get Returns GET data.
  • /status/:code Returns given HTTP Status code.
  • /redirect/:n 302 Redirects n times.
  • /absolute-redirect/:n 302 Absolute redirects n times.
  • /redirect-to?url=foo 302 Redirects to the foo URL.
  • /stream/:n Streams n lines of JSON objects
  • /delay/:n Delays responding for min(n, 10) seconds.
  • /bytes/:n Generates n random bytes of binary data, accepts optional seed integer parameter.
  • /cookies Returns the cookies.
  • /cookies/set?name=value Sets one or more simple cookies.
  • /cookies/delete?name Deletes one or more simple cookies.
  • /drip?numbytes=n&duration=s&delay=s&code=code Drips data over a duration after an optional initial delay, then optionally returns with the given status code.
  • /cache Returns 200 unless an If-Modified-Since or If-None-Match header is provided, when it returns a 304.
  • /cache/:n Sets a Cache-Control header for n seconds.

How to use

Standing up a Go server running httpbin endpoints is just 1 line:

package main

import (
    "log"
    "net/http"
    "github.com/ahmetalpbalkan/go-httpbin"
)

func main() {
	log.Fatal(http.ListenAndServe(":8080", httpbin.GetMux()))
}

Let's say you do not want a server running all the time because you just want to test your HTTP logic after all. Integrating httpbin to your tests is very simple:

package test

import (
    "testing"
    "net/http"
    "net/http/httptest"

    "github.com/ahmetalpbalkan/go-httpbin"
)

func TestDownload(t *testing.T) {
    srv := httptest.NewServer(httpbin.GetMux())
    defer srv.Close()

    resp, err := http.Get(srv.URL + "/bytes/65536")
    if err != nil {
        t.Fatal(err)
    }
    // read from an actual HTTP server hosted locally
    // test whatever you are going to test...
}

License

Copyright 2016 Ahmet Alp Balkan

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Authors

go-httpbin's People

Contributors

ahmetb 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.