blob: f8016a03e0c25b8b01ee41fc52e0befb24850f02 [file] [log] [blame]
Serge Bazanski3638a3d2021-05-23 18:25:46 +02001package ident
2
3import (
4 "context"
5 "errors"
6 "net"
7 "testing"
8)
9
10// TestE2E performs an end-to-end test of the ident client and server,
11// exercising as much functionality as possible.
12func TestE2E(t *testing.T) {
13 lis, err := net.Listen("tcp", "127.0.0.1:0")
14 if err != nil {
15 t.Fatalf("Listen: %v", err)
16 }
17 defer lis.Close()
18
19 isrv := NewServer()
20 isrv.HandleFunc(func(ctx context.Context, w ResponseWriter, r *Request) {
21 switch r.ServerPort {
22 case 1:
23 w.SendError(NoUser)
24 default:
25 w.SendIdent(&IdentResponse{
26 UserID: "q3k",
27 })
28 }
29 })
30 go isrv.Serve(lis)
31 target := lis.Addr().String()
32
33 ctx := context.Background()
34
35 // Full end-to-end test, from high-level client Query call, to server
36 // handler.
37
38 // This call should succeed.
39 resp, err := Query(ctx, target, 123, 234)
40 if err != nil {
41 t.Fatalf("Query: %v", err)
42 }
43 if want, got := "q3k", resp.UserID; want != got {
44 t.Errorf("Resp.UserID: wanted %q, got %q", want, got)
45 }
46
47 // This call should error out and return an error that can be converted
48 // back into ErrorResponse.
49 resp, err = Query(ctx, target, 123, 1)
50 if err == nil {
51 t.Fatalf("Query returned nil error")
52 }
53 identErr := &IdentError{}
54 if errors.As(err, &identErr) {
55 if want, got := NoUser, identErr.Inner; want != got {
56 t.Errorf("Query error is %q, wanted %q", want, got)
57 }
58 } else {
59 t.Errorf("Query error did not match AnyError")
60 }
61 // Test matches against specific errors.
62 if errors.Is(err, &IdentError{UnknownError}) {
63 t.Errorf("Query error should not have matched UnknownError")
64 }
65 if !errors.Is(err, &IdentError{NoUser}) {
66 t.Errorf("Query error should have matcher NoUser")
67 }
68}