@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 10, 22:30 UTC

master
go 93 lines 2.15 KB
Raw
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package caddy
16
17 import (
18 "context"
19 "testing"
20 "time"
21 )
22
23 func TestParseDuration(t *testing.T) {
24 const day = 24 * time.Hour
25 for i, tc := range []struct {
26 input string
27 expect time.Duration
28 }{
29 {
30 input: "3h",
31 expect: 3 * time.Hour,
32 },
33 {
34 input: "1d",
35 expect: day,
36 },
37 {
38 input: "1d30m",
39 expect: day + 30*time.Minute,
40 },
41 {
42 input: "1m2d",
43 expect: time.Minute + day*2,
44 },
45 {
46 input: "1m2d30s",
47 expect: time.Minute + day*2 + 30*time.Second,
48 },
49 {
50 input: "1d2d",
51 expect: 3 * day,
52 },
53 {
54 input: "1.5d",
55 expect: time.Duration(1.5 * float64(day)),
56 },
57 {
58 input: "4m1.25d",
59 expect: 4*time.Minute + time.Duration(1.25*float64(day)),
60 },
61 {
62 input: "-1.25d12h",
63 expect: time.Duration(-1.25*float64(day)) - 12*time.Hour,
64 },
65 } {
66 actual, err := ParseDuration(tc.input)
67 if err != nil {
68 t.Errorf("Test %d ('%s'): Got error: %v", i, tc.input, err)
69 continue
70 }
71 if actual != tc.expect {
72 t.Errorf("Test %d ('%s'): Expected=%s Actual=%s", i, tc.input, tc.expect, actual)
73 }
74 }
75 }
76
77 func TestEvent_CloudEvent_NilOrigin(t *testing.T) {
78 ctx, _ := NewContext(Context{Context: context.Background()}) // module will be nil by default
79 event, err := NewEvent(ctx, "started", nil)
80 if err != nil {
81 t.Fatalf("NewEvent() error = %v", err)
82 }
83
84 // This should not panic
85 ce := event.CloudEvent()
86
87 if ce.Source != "caddy" {
88 t.Errorf("Expected CloudEvent Source to be 'caddy', got '%s'", ce.Source)
89 }
90 if ce.Type != "started" {
91 t.Errorf("Expected CloudEvent Type to be 'started', got '%s'", ce.Type)
92 }
93 }