File: error_test.go

package info (click to toggle)
golang-github-apptainer-container-key-client 0.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 160 kB
  • sloc: makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,253 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
50
51
52
// Copyright (c) 2020, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.

package client

import (
	"errors"
	"net/http"
	"testing"
)

func TestHTTPError(t *testing.T) {
	tests := []struct {
		name        string
		code        int
		err         error
		wantMessage string
	}{
		{
			name:        "BadRequest",
			code:        http.StatusBadRequest,
			wantMessage: "400 Bad Request",
		},
		{
			name:        "BadRequestWithMessage",
			code:        http.StatusBadRequest,
			err:         errors.New("more good needed"),
			wantMessage: "400 Bad Request: more good needed",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := &HTTPError{
				code: tt.code,
				err:  tt.err,
			}

			if got, want := err.Code(), tt.code; got != want {
				t.Errorf("got code %v, want %v", got, want)
			}
			if got, want := err.Unwrap(), tt.err; got != want {
				t.Errorf("got unwrapped error %v, want %v", got, want)
			}
			if got, want := err.Error(), tt.wantMessage; got != want {
				t.Errorf("got message %v, want %v", got, want)
			}
		})
	}
}