Skip to content

Instantly share code, notes, and snippets.

@dekokun
Last active September 2, 2016 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dekokun/4f66e00f80b7da4107140d15de9188d3 to your computer and use it in GitHub Desktop.
Save dekokun/4f66e00f80b7da4107140d15de9188d3 to your computer and use it in GitHub Desktop.
Varnishのテストとかに使えるいい感じにオウム返しするサーバ
  • proxy的な働きをするサーバが後段に対してどのようなヘッダでリクエストを行うかを知りたい
  • 後段のサーバからどのようなヘッダが返った際にproxy的な働きをするサーバはどのような動きをするかを知りたい

という調査が簡単になるために作った。

機能

  • リクエストのクエリストリングがHTTPヘッダになる
    • 後段のサーバに任意のHTTPヘッダを返させてproxyの挙動を見ることができる
  • リクエストのHTTPヘッダはjsonになる
    • 後段のサーバにどのようなHTTPヘッダが渡っているかの調査を行うことができる
$ curl -IXGET localhost:30000/?hoge=fuga\&gege=gogo\&gege=bebe\&Cache-Control=max-age=25\&Cache-Control=public
HTTP/1.1 200 OK
Cache-Control: max-age=25,public
Content-Type: application/json; charset=utf-8
Gege: gogo,bebe
Hoge: fuga
Date: Tue, 28 Jun 2016 09:21:47 GMT
Content-Length: 74
$ curl --header 'Hoge: Fuga' localhost:30000/ | jq '.'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    90  100    90    0     0  14206      0 --:--:-- --:--:-- --:--:-- 15000
{
  "Accept": [
    "*/*"
  ],
  "Hoge": [
    "Fuga"
  ],
  "Host": [
    "localhost:30000"
  ],
  "User-Agent": [
    "curl/7.38.0"
  ]
}

TODO

  • portを可変に
  • githubにあげる
  • Content-Typeも指定可能に
package main
import (
"encoding/json"
"log"
"net/http"
"strings"
)
func rootHandler(w http.ResponseWriter, r *http.Request) {
headers := r.Header
headers["Host"] = []string{r.Host}
json, _ := json.Marshal(r.Header)
log.Print(r.URL.Query())
for k, vs := range r.URL.Query() {
values := strings.Join(vs, ",")
w.Header().Set(k, values)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
w.Write(json)
log.Print("get request")
}
func main() {
http.HandleFunc("/", rootHandler)
log.Fatal(http.ListenAndServe("localhost:30000", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment