File: example_test.go

package info (click to toggle)
golang-github-muhlemmer-httpforwarded 0.1.0-2~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 116 kB
  • sloc: makefile: 2
file content (49 lines) | stat: -rw-r--r-- 1,263 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2016 Tim Heckman. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package httpforwarded_test

import (
	"fmt"

	"github.com/muhlemmer/httpforwarded"
)

func ExampleParse() {
	// mock value of HTTP headers
	headers := []string{"for=192.0.2.1; proto=http"}

	// parse the fields in to one map
	params, _ := httpforwarded.Parse(headers)

	// print the origin IP address and protocolg
	fmt.Printf("origin: %s | protocol: %s", params["for"][0], params["proto"][0])
	// output: origin: 192.0.2.1 | protocol: http
}

func ExampleFormat() {
	// build a parameter map
	params := map[string][]string{
		"for":   []string{"192.0.2.1", "192.0.2.4"},
		"proto": []string{"http"},
	}

	// format the parameter map
	val := httpforwarded.Format(params)

	fmt.Print(val)
	// output: for=192.0.2.1, for=192.0.2.4; proto=http
}

func ExampleParseParameter() {
	// mock value of HTTP headers
	headers := []string{"for=192.0.2.1, for=192.0.2.42; proto=http"}

	// parse the header fields while only extracting 'for' parameter
	values, _ := httpforwarded.ParseParameter("for", headers)

	// print the slice of values
	fmt.Printf("%#v", values)
	// output: []string{"192.0.2.1", "192.0.2.42"}
}