Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: env /tags endpoint and env based HTTP headers #565

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

simonkowallik
Copy link

@simonkowallik simonkowallik commented Jul 9, 2019

Description

This feature adds a /tags endpoint and inserts HTTP headers, both based on environment variables. The new endpoint returns environment variables (and their values) starting with HTTPBIN_.
XHTTPBIN_ environment variables are added as HTTP response headers.
HTTPBIN_ as well as XHTTPBIN_ will be stripped from the name, additionally all underscores _ in the XHTTPBIN_ variable name will be converted to a dash - for better shell variable name and HTTP header support.

Motivation

The motivation is that load balancing/horizontal scaling tests usually require unique identification of the instance handling the request. This feature allows to uniquely tag these instances (containers or even multiple httpbin instances on the same server OS). The optional HTTP header provides this identifier across the whole httpbin feature set, not only for the /tags endpoint.

Examples

Assuming the following environment variables are set:

  • XHTTPBIN_X_TagHeader="Header Value"
  • HTTPBIN_Tag=Value
  • HTTPBIN_AnotherTag="long value..."
> curl -i http://localhost/tags
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 55
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-TagHeader: Header Value
Server: Unit/1.9.0
Date: Sat, 06 Jul 2019 11:53:18 GMT

{
  "AnotherTag": "long value...",
  "Tag": "Value"
}

> curl -i http://localhost/tags/get/Tag
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 21
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-TagHeader: Header Value
Server: Unit/1.9.0
Date: Sat, 06 Jul 2019 11:54:40 GMT

{
  "Tag": "Value"
}

> curl -i http://localhost/tags/get/UndefinedTag
HTTP/1.1 404 Not Found
Content-Type: application/json
Content-Length: 2
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-TagHeader: Header Value
Server: Unit/1.9.0
Date: Sat, 06 Jul 2019 11:55:03 GMT

{}

> curl -i http://localhost/get
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 179
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-TagHeader: Header Value
Server: Unit/1.9.0
Date: Sat, 06 Jul 2019 11:55:25 GMT

{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Host": "localhost",
    "User-Agent": "curl/7.54.0"
  },
  "origin": "172.17.0.1",
  "url": "http://localhost/get"
}

If no HTTPBIN_ environment variables exist, the response for /tags will look like this:

> curl -i http://localhost/tags
HTTP/1.1 404 Not Found
Content-Type: application/json
Content-Length: 2
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-TagHeader: Header Value
Server: Unit/1.9.0
Date: Sat, 06 Jul 2019 11:58:07 GMT

{}

use _setenv as a context manager (with _setenv ... :) instead
of a decorator (@_setenv ...)

Since python3.2+ contextlib.contextmanager uses ContextDecorator
allowing the decorated function _setenv to be used as a decorator
in addition to context manager.

contextlib in python2.7 doesn't have ContextDecorator, therefore
_setenv must be used as a context manager not a decorator.
@simonkowallik simonkowallik changed the title Feature: env /tags endpoint and env Headers Feature: env /tags endpoint and env based HTTP headers Jul 9, 2019
@simonkowallik
Copy link
Author

simonkowallik commented Jul 9, 2019

This pull request is similar to and inspired by #552

Issue #558 and pull request #564 also show unique identification of instances is an often required feature.

@chinaran
Copy link

good pr, hope merged

@dmuth
Copy link

dmuth commented Jul 25, 2021

I just found this and would love to see it merged, as I'm playing around with multiple httpbin instances, and having a way to uniquely identify each one would be super helpful.

@chinaran
Copy link

chinaran commented Aug 1, 2021

@dmuth You could try https://github.com/chinaran/go-httpbin, a golang port of httpbin.
I added envs param for identify httpbin instance. It will default show HOSTNAME env which in k8s is pod name.

@dmuth
Copy link

dmuth commented Aug 8, 2021

@dmuth You could try https://github.com/chinaran/go-httpbin, a golang port of httpbin.
I added envs param for identify httpbin instance. It will default show HOSTNAME env which in k8s is pod name.

That worked for me, thanks for sharing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants