84adf3603f2da6743524bb29f57e700f8aeebfff
[arvados.git] / services / keepstore / server_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "bytes"
9         "context"
10         "crypto/tls"
11         "io/ioutil"
12         "net"
13         "net/http"
14         "testing"
15 )
16
17 func TestTLS(t *testing.T) {
18         defer func() {
19                 theConfig.TLSKeyFile = ""
20                 theConfig.TLSCertificateFile = ""
21         }()
22         theConfig.TLSKeyFile = "../api/tmp/self-signed.key"
23         theConfig.TLSCertificateFile = "../api/tmp/self-signed.pem"
24         srv := &server{}
25         srv.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
26                 w.Write([]byte("OK"))
27         })
28         l, err := net.Listen("tcp", ":")
29         if err != nil {
30                 t.Fatal(err)
31         }
32         defer l.Close()
33         go srv.Serve(l)
34         defer srv.Shutdown(context.Background())
35         c := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
36         resp, err := c.Get("https://" + l.Addr().String() + "/")
37         if err != nil {
38                 t.Fatal(err)
39         }
40         body, err := ioutil.ReadAll(resp.Body)
41         if err != nil {
42                 t.Error(err)
43         }
44         if !bytes.Equal(body, []byte("OK")) {
45                 t.Errorf("expected OK, got %q", body)
46         }
47 }