~ancarda/tls-redirector

ref: 5f0b9ad56965a4fa1f18e3734f574c80e6e7340f tls-redirector/cli_test.go -rw-r--r-- 1.5 KiB
5f0b9ad5Mark Dain Add a basic Docker healthcheck 2 months ago
                                                                                
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
50
51
52
53
54
55
56
57
58
59
package main

import (
	"bytes"
	"regexp"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestCLIVersionFlag(t *testing.T) {
	var buf bytes.Buffer
	args := []string{"./test", "--version"}

	assert.Equal(t, 0, handleCliArgs(&buf, args, false))
	assert.Equal(t, "tls redirector 2.4\n", buf.String())
}

func TestCLIUnknownFlag(t *testing.T) {
	var buf bytes.Buffer
	args := []string{"./test", "--no-such-flag"}

	assert.Equal(t, 1, handleCliArgs(&buf, args, false))
	assert.Equal(t, "unrecognized: --no-such-flag\n", buf.String())
}

func TestCLIHelpFlag(t *testing.T) {
	var buf bytes.Buffer
	args := []string{"./test", "--help"}

	assert.Equal(t, 0, handleCliArgs(&buf, args, false))

	strings := []string{
		"Usage: ./test [OPTION]\n",
		"Compiled without support for systemd socket activation.\n",
	}
	for _, s := range strings {
		assert.Contains(t, buf.String(), s)
	}

	regexes := []string{
		"--help\\s+display this help and exit",
		"--version\\s+output version information and exit",
		"PORT\\s+- TCP/IP port number to listen on",
		"ACME_CHALLENGE_DIR\\s+- Directory to serve at /.well-known/acme-challenge",
	}
	for _, r := range regexes {
		assert.Regexp(t, regexp.MustCompile(r), buf.String())
	}
	assert.Regexp(t, regexp.MustCompile("--help\\s+display this help and exit"), buf.String())
}

func TestCLIHelpFlag_WithSocketActivation(t *testing.T) {
	var buf bytes.Buffer
	args := []string{"./test", "--help"}

	assert.Equal(t, 0, handleCliArgs(&buf, args, true))
	assert.Contains(t, buf.String(), "Compiled with support for systemd socket activation.")
}