-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathinput_test.go
More file actions
56 lines (48 loc) · 1.31 KB
/
input_test.go
File metadata and controls
56 lines (48 loc) · 1.31 KB
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
package server
import (
"bytes"
"context"
"fmt"
"net"
"net/http"
"os"
"testing"
"github.com/dependabot/cli/internal/model"
)
func TestInput(t *testing.T) {
inputCh := make(chan *model.Input)
defer close(inputCh)
ip := ""
// prevents security popup
if os.Getenv("GOOS") == "darwin" {
ip = "127.0.0.1"
}
l, err := (&net.ListenConfig{}).Listen(context.Background(), "tcp", ip+":0")
if err != nil {
t.Fatal("Failed to create listener: ", err.Error())
}
go func() {
input, err := Input(l)
if err != nil {
t.Errorf("%s", err.Error())
}
inputCh <- input
}()
url := fmt.Sprintf("http://%s", l.Addr().String())
data := `{"job":{"package-manager":"test"},"credentials":[{"credential":"value"}]}`
resp, err := http.Post(url, "application/json", bytes.NewReader([]byte(data))) //nolint:gosec // test code with controlled URL
if err != nil {
t.Fatal(err.Error())
}
if resp.StatusCode != 200 {
t.Errorf("expected status code 200, got %d", resp.StatusCode)
}
// Test will hang here if the server does not shut down
input := <-inputCh
if input.Job.PackageManager != "test" {
t.Errorf("expected package manager to be 'test', got '%s'", input.Job.PackageManager)
}
if input.Credentials[0]["credential"] != "value" {
t.Errorf("expected credential to be 'value', got '%v'", input.Credentials[0])
}
}