Rewrote Caddy from the ground up; initial commit of 0.9 branch
These changes span work from the last ~4 months in an effort to make
Caddy more extensible, reduce the coupling between its components, and
lay a more robust foundation of code going forward into 1.0. A bunch of
new features have been added, too, with even higher future potential.
The most significant design change is an overall inversion of
dependencies. Instead of the caddy package knowing about the server
and the notion of middleware and config, the caddy package exposes an
interface that other components plug into. This does introduce more
indirection when reading the code, but every piece is very modular and
pluggable. Even the HTTP server is pluggable.
The caddy package has been moved to the top level, and main has been
pushed into a subfolder called caddy. The actual logic of the main
file has been pushed even further into caddy/caddymain/run.go so that
custom builds of Caddy can be 'go get'able.
The HTTPS logic was surgically separated into two parts to divide the
TLS-specific code and the HTTPS-specific code. The caddytls package can
now be used by any type of server that needs TLS, not just HTTP. I also
added the ability to customize nearly every aspect of TLS at the site
level rather than all sites sharing the same TLS configuration. Not all
of this flexibility is exposed in the Caddyfile yet, but it may be in
the future. Caddy can also generate self-signed certificates in memory
for the convenience of a developer working on localhost who wants HTTPS.
And Caddy now supports the DNS challenge, assuming at least one DNS
provider is plugged in.
Dozens, if not hundreds, of other minor changes swept through the code
base as I literally started from an empty main function, copying over
functions or files as needed, then adjusting them to fit in the new
design. Most tests have been restored and adapted to the new API,
but more work is needed there.
A lot of what was "impossible" before is now possible, or can be made
possible with minimal disruption of the code. For example, it's fairly
easy to make plugins hook into another part of the code via callbacks.
Plugins can do more than just be directives; we now have plugins that
customize how the Caddyfile is loaded (useful when you need to get your
configuration from a remote store).
Site addresses no longer need be just a host and port. They can have a
path, allowing you to scope a configuration to a specific path. There is
no inheretance, however; each site configuration is distinct.
Thanks to amazing work by Lucas Clemente, this commit adds experimental
QUIC support. Turn it on using the -quic flag; your browser may have
to be configured to enable it.
Almost everything is here, but you will notice that most of the middle-
ware are missing. After those are transferred over, we'll be ready for
beta tests.
I'm very excited to get this out. Thanks for everyone's help and
patience these last few months. I hope you like it!!
2016-06-04 18:00:29 -05:00
|
|
|
package httpserver
|
2015-08-27 13:06:32 -05:00
|
|
|
|
|
|
|
import (
|
2017-03-11 16:59:47 -05:00
|
|
|
"context"
|
2015-08-27 13:06:32 -05:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2016-04-16 12:03:56 -05:00
|
|
|
"os"
|
2015-08-27 13:06:32 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-08-08 11:14:53 -05:00
|
|
|
"time"
|
2015-08-27 13:06:32 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewReplacer(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
recordRequest := NewResponseRecorder(w)
|
2015-12-31 14:12:16 -05:00
|
|
|
reader := strings.NewReader(`{"username": "dennis"}`)
|
2015-08-27 13:06:32 -05:00
|
|
|
|
2015-12-31 14:12:16 -05:00
|
|
|
request, err := http.NewRequest("POST", "http://localhost", reader)
|
2015-08-27 13:06:32 -05:00
|
|
|
if err != nil {
|
2015-12-31 14:12:16 -05:00
|
|
|
t.Fatal("Request Formation Failed\n")
|
2015-08-27 13:06:32 -05:00
|
|
|
}
|
2016-03-20 22:56:13 -05:00
|
|
|
rep := NewReplacer(request, recordRequest, "")
|
2015-09-09 23:58:13 -05:00
|
|
|
|
2016-03-20 22:56:13 -05:00
|
|
|
switch v := rep.(type) {
|
|
|
|
case *replacer:
|
2016-08-18 21:36:10 -05:00
|
|
|
if v.getSubstitution("{host}") != "localhost" {
|
2015-12-31 14:12:16 -05:00
|
|
|
t.Error("Expected host to be localhost")
|
2015-08-27 13:06:32 -05:00
|
|
|
}
|
2016-08-18 21:36:10 -05:00
|
|
|
if v.getSubstitution("{method}") != "POST" {
|
2015-12-31 14:12:16 -05:00
|
|
|
t.Error("Expected request method to be POST")
|
2015-08-27 13:06:32 -05:00
|
|
|
}
|
|
|
|
default:
|
2016-03-20 22:56:13 -05:00
|
|
|
t.Fatalf("Expected *replacer underlying Replacer type, got: %#v", rep)
|
2015-08-27 13:06:32 -05:00
|
|
|
}
|
|
|
|
}
|
2015-09-09 23:58:13 -05:00
|
|
|
|
|
|
|
func TestReplace(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
recordRequest := NewResponseRecorder(w)
|
2015-12-31 14:12:16 -05:00
|
|
|
reader := strings.NewReader(`{"username": "dennis"}`)
|
2015-09-09 23:58:13 -05:00
|
|
|
|
2017-03-03 01:25:28 -05:00
|
|
|
request, err := http.NewRequest("POST", "http://localhost/?foo=bar", reader)
|
2015-09-09 23:58:13 -05:00
|
|
|
if err != nil {
|
httpserver/all: Clean up and standardize request URL handling (#1633)
* httpserver/all: Clean up and standardize request URL handling
The HTTP server now always creates a context value on the request which
is a copy of the request's URL struct. It should not be modified by
middlewares, but it is safe to get the value out of the request and make
changes to it locally-scoped. Thus, the value in the context always
stores the original request URL information as it was received. Any
rewrites that happen will be to the request's URL field directly.
The HTTP server no longer cleans /sanitizes the request URL. It made too
many strong assumptions and ended up making a lot of middleware more
complicated, including upstream proxying (and fastcgi). To alleviate
this complexity, we no longer change the request URL. Middlewares are
responsible to access the disk safely by using http.Dir or, if not
actually opening files, they can use httpserver.SafePath().
I'm hoping this will address issues with #1624, #1584, #1582, and others.
* staticfiles: Fix test on Windows
@abiosoft: I still can't figure out exactly what this is for. 😅
* Use (potentially) changed URL for browse redirects, as before
* Use filepath.ToSlash, clean up a couple proxy test cases
* Oops, fix variable name
2017-05-02 00:11:10 -05:00
|
|
|
t.Fatalf("Failed to make request: %v", err)
|
2015-09-09 23:58:13 -05:00
|
|
|
}
|
httpserver/all: Clean up and standardize request URL handling (#1633)
* httpserver/all: Clean up and standardize request URL handling
The HTTP server now always creates a context value on the request which
is a copy of the request's URL struct. It should not be modified by
middlewares, but it is safe to get the value out of the request and make
changes to it locally-scoped. Thus, the value in the context always
stores the original request URL information as it was received. Any
rewrites that happen will be to the request's URL field directly.
The HTTP server no longer cleans /sanitizes the request URL. It made too
many strong assumptions and ended up making a lot of middleware more
complicated, including upstream proxying (and fastcgi). To alleviate
this complexity, we no longer change the request URL. Middlewares are
responsible to access the disk safely by using http.Dir or, if not
actually opening files, they can use httpserver.SafePath().
I'm hoping this will address issues with #1624, #1584, #1582, and others.
* staticfiles: Fix test on Windows
@abiosoft: I still can't figure out exactly what this is for. 😅
* Use (potentially) changed URL for browse redirects, as before
* Use filepath.ToSlash, clean up a couple proxy test cases
* Oops, fix variable name
2017-05-02 00:11:10 -05:00
|
|
|
ctx := context.WithValue(request.Context(), OriginalURLCtxKey, *request.URL)
|
|
|
|
request = request.WithContext(ctx)
|
|
|
|
|
2015-12-31 14:31:30 -05:00
|
|
|
request.Header.Set("Custom", "foobarbaz")
|
|
|
|
request.Header.Set("ShorterVal", "1")
|
2015-12-31 14:12:16 -05:00
|
|
|
repl := NewReplacer(request, recordRequest, "-")
|
2016-09-28 03:54:53 -05:00
|
|
|
// add some headers after creating replacer
|
|
|
|
request.Header.Set("CustomAdd", "caddy")
|
2017-02-15 23:59:24 -05:00
|
|
|
request.Header.Set("Cookie", "foo=bar; taste=delicious")
|
2015-09-09 23:58:13 -05:00
|
|
|
|
2016-04-16 12:03:56 -05:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
httpserver/all: Clean up and standardize request URL handling (#1633)
* httpserver/all: Clean up and standardize request URL handling
The HTTP server now always creates a context value on the request which
is a copy of the request's URL struct. It should not be modified by
middlewares, but it is safe to get the value out of the request and make
changes to it locally-scoped. Thus, the value in the context always
stores the original request URL information as it was received. Any
rewrites that happen will be to the request's URL field directly.
The HTTP server no longer cleans /sanitizes the request URL. It made too
many strong assumptions and ended up making a lot of middleware more
complicated, including upstream proxying (and fastcgi). To alleviate
this complexity, we no longer change the request URL. Middlewares are
responsible to access the disk safely by using http.Dir or, if not
actually opening files, they can use httpserver.SafePath().
I'm hoping this will address issues with #1624, #1584, #1582, and others.
* staticfiles: Fix test on Windows
@abiosoft: I still can't figure out exactly what this is for. 😅
* Use (potentially) changed URL for browse redirects, as before
* Use filepath.ToSlash, clean up a couple proxy test cases
* Oops, fix variable name
2017-05-02 00:11:10 -05:00
|
|
|
t.Fatalf("Failed to determine hostname: %v", err)
|
2016-04-16 12:03:56 -05:00
|
|
|
}
|
2015-09-09 23:58:13 -05:00
|
|
|
|
2017-01-14 17:54:27 -05:00
|
|
|
old := now
|
|
|
|
now = func() time.Time {
|
|
|
|
return time.Date(2006, 1, 2, 15, 4, 5, 02, time.FixedZone("hardcoded", -7))
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
now = old
|
|
|
|
}()
|
2016-06-21 19:44:16 -05:00
|
|
|
testCases := []struct {
|
|
|
|
template string
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{"This hostname is {hostname}", "This hostname is " + hostname},
|
|
|
|
{"This host is {host}.", "This host is localhost."},
|
|
|
|
{"This request method is {method}.", "This request method is POST."},
|
|
|
|
{"The response status is {status}.", "The response status is 200."},
|
2017-01-14 17:54:27 -05:00
|
|
|
{"{when}", "02/Jan/2006:15:04:05 +0000"},
|
|
|
|
{"{when_iso}", "2006-01-02T15:04:12Z"},
|
2017-07-05 14:43:54 -05:00
|
|
|
{"{when_unix}", "1136214252"},
|
2016-06-21 19:44:16 -05:00
|
|
|
{"The Custom header is {>Custom}.", "The Custom header is foobarbaz."},
|
2016-09-28 03:54:53 -05:00
|
|
|
{"The CustomAdd header is {>CustomAdd}.", "The CustomAdd header is caddy."},
|
2017-03-03 01:25:28 -05:00
|
|
|
{"The request is {request}.", "The request is POST /?foo=bar HTTP/1.1\\r\\nHost: localhost\\r\\n" +
|
2017-02-15 23:59:24 -05:00
|
|
|
"Cookie: foo=bar; taste=delicious\\r\\nCustom: foobarbaz\\r\\nCustomadd: caddy\\r\\n" +
|
|
|
|
"Shorterval: 1\\r\\n\\r\\n."},
|
2016-06-21 19:44:16 -05:00
|
|
|
{"The cUsToM header is {>cUsToM}...", "The cUsToM header is foobarbaz..."},
|
|
|
|
{"The Non-Existent header is {>Non-Existent}.", "The Non-Existent header is -."},
|
|
|
|
{"Bad {host placeholder...", "Bad {host placeholder..."},
|
|
|
|
{"Bad {>Custom placeholder", "Bad {>Custom placeholder"},
|
|
|
|
{"Bad {>Custom placeholder {>ShorterVal}", "Bad -"},
|
2017-02-15 23:59:24 -05:00
|
|
|
{"Bad {}", "Bad -"},
|
|
|
|
{"Cookies are {~taste}", "Cookies are delicious"},
|
|
|
|
{"Missing cookie is {~missing}", "Missing cookie is -"},
|
2017-03-03 01:25:28 -05:00
|
|
|
{"Query string is {query}", "Query string is foo=bar"},
|
|
|
|
{"Query string value for foo is {?foo}", "Query string value for foo is bar"},
|
|
|
|
{"Missing query string argument is {?missing}", "Missing query string argument is "},
|
2016-06-21 19:44:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range testCases {
|
|
|
|
if expected, actual := c.expect, repl.Replace(c.template); expected != actual {
|
|
|
|
t.Errorf("for template '%s', expected '%s', got '%s'", c.template, expected, actual)
|
|
|
|
}
|
2015-12-31 14:31:30 -05:00
|
|
|
}
|
|
|
|
|
2016-06-21 19:44:16 -05:00
|
|
|
complexCases := []struct {
|
|
|
|
template string
|
2016-08-18 21:36:10 -05:00
|
|
|
replacements map[string]string
|
2016-06-21 19:44:16 -05:00
|
|
|
expect string
|
|
|
|
}{
|
2016-08-18 21:36:10 -05:00
|
|
|
{
|
|
|
|
"/a{1}/{2}",
|
|
|
|
map[string]string{
|
|
|
|
"{1}": "12",
|
|
|
|
"{2}": "",
|
|
|
|
},
|
2016-07-20 20:06:14 -05:00
|
|
|
"/a12/"},
|
2015-12-31 14:31:30 -05:00
|
|
|
}
|
|
|
|
|
2016-06-21 19:44:16 -05:00
|
|
|
for _, c := range complexCases {
|
|
|
|
repl := &replacer{
|
2016-08-18 21:36:10 -05:00
|
|
|
customReplacements: c.replacements,
|
2016-06-21 19:44:16 -05:00
|
|
|
}
|
|
|
|
if expected, actual := c.expect, repl.Replace(c.template); expected != actual {
|
|
|
|
t.Errorf("for template '%s', expected '%s', got '%s'", c.template, expected, actual)
|
|
|
|
}
|
2015-12-31 14:12:16 -05:00
|
|
|
}
|
2015-09-09 23:58:13 -05:00
|
|
|
}
|
2015-12-30 14:42:03 -05:00
|
|
|
|
|
|
|
func TestSet(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
recordRequest := NewResponseRecorder(w)
|
2015-12-31 14:12:16 -05:00
|
|
|
reader := strings.NewReader(`{"username": "dennis"}`)
|
2015-12-30 14:42:03 -05:00
|
|
|
|
2015-12-31 14:12:16 -05:00
|
|
|
request, err := http.NewRequest("POST", "http://localhost", reader)
|
2015-12-30 14:42:03 -05:00
|
|
|
if err != nil {
|
2016-08-15 12:15:58 -05:00
|
|
|
t.Fatalf("Request Formation Failed: %s\n", err.Error())
|
2015-12-30 14:42:03 -05:00
|
|
|
}
|
2015-12-31 14:12:16 -05:00
|
|
|
repl := NewReplacer(request, recordRequest, "")
|
2015-12-30 14:42:03 -05:00
|
|
|
|
2015-12-31 14:12:16 -05:00
|
|
|
repl.Set("host", "getcaddy.com")
|
|
|
|
repl.Set("method", "GET")
|
|
|
|
repl.Set("status", "201")
|
|
|
|
repl.Set("variable", "value")
|
2015-12-30 14:42:03 -05:00
|
|
|
|
2015-12-31 14:12:16 -05:00
|
|
|
if repl.Replace("This host is {host}") != "This host is getcaddy.com" {
|
|
|
|
t.Error("Expected host replacement failed")
|
|
|
|
}
|
|
|
|
if repl.Replace("This request method is {method}") != "This request method is GET" {
|
|
|
|
t.Error("Expected method replacement failed")
|
|
|
|
}
|
|
|
|
if repl.Replace("The response status is {status}") != "The response status is 201" {
|
|
|
|
t.Error("Expected status replacement failed")
|
|
|
|
}
|
|
|
|
if repl.Replace("The value of variable is {variable}") != "The value of variable is value" {
|
|
|
|
t.Error("Expected variable replacement failed")
|
2015-12-30 14:42:03 -05:00
|
|
|
}
|
|
|
|
}
|
2016-08-08 11:14:53 -05:00
|
|
|
|
2017-03-11 16:59:47 -05:00
|
|
|
// Test function to test that various placeholders hold correct values after a rewrite
|
|
|
|
// has been performed. The NewRequest actually contains the rewritten value.
|
|
|
|
func TestPathRewrite(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
recordRequest := NewResponseRecorder(w)
|
|
|
|
reader := strings.NewReader(`{"username": "dennis"}`)
|
|
|
|
|
|
|
|
request, err := http.NewRequest("POST", "http://getcaddy.com/index.php?key=value", reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Request Formation Failed: %s\n", err.Error())
|
|
|
|
}
|
httpserver/all: Clean up and standardize request URL handling (#1633)
* httpserver/all: Clean up and standardize request URL handling
The HTTP server now always creates a context value on the request which
is a copy of the request's URL struct. It should not be modified by
middlewares, but it is safe to get the value out of the request and make
changes to it locally-scoped. Thus, the value in the context always
stores the original request URL information as it was received. Any
rewrites that happen will be to the request's URL field directly.
The HTTP server no longer cleans /sanitizes the request URL. It made too
many strong assumptions and ended up making a lot of middleware more
complicated, including upstream proxying (and fastcgi). To alleviate
this complexity, we no longer change the request URL. Middlewares are
responsible to access the disk safely by using http.Dir or, if not
actually opening files, they can use httpserver.SafePath().
I'm hoping this will address issues with #1624, #1584, #1582, and others.
* staticfiles: Fix test on Windows
@abiosoft: I still can't figure out exactly what this is for. 😅
* Use (potentially) changed URL for browse redirects, as before
* Use filepath.ToSlash, clean up a couple proxy test cases
* Oops, fix variable name
2017-05-02 00:11:10 -05:00
|
|
|
urlCopy := *request.URL
|
|
|
|
urlCopy.Path = "a/custom/path.php"
|
|
|
|
ctx := context.WithValue(request.Context(), OriginalURLCtxKey, urlCopy)
|
2017-03-11 16:59:47 -05:00
|
|
|
request = request.WithContext(ctx)
|
|
|
|
|
|
|
|
repl := NewReplacer(request, recordRequest, "")
|
|
|
|
|
httpserver/all: Clean up and standardize request URL handling (#1633)
* httpserver/all: Clean up and standardize request URL handling
The HTTP server now always creates a context value on the request which
is a copy of the request's URL struct. It should not be modified by
middlewares, but it is safe to get the value out of the request and make
changes to it locally-scoped. Thus, the value in the context always
stores the original request URL information as it was received. Any
rewrites that happen will be to the request's URL field directly.
The HTTP server no longer cleans /sanitizes the request URL. It made too
many strong assumptions and ended up making a lot of middleware more
complicated, including upstream proxying (and fastcgi). To alleviate
this complexity, we no longer change the request URL. Middlewares are
responsible to access the disk safely by using http.Dir or, if not
actually opening files, they can use httpserver.SafePath().
I'm hoping this will address issues with #1624, #1584, #1582, and others.
* staticfiles: Fix test on Windows
@abiosoft: I still can't figure out exactly what this is for. 😅
* Use (potentially) changed URL for browse redirects, as before
* Use filepath.ToSlash, clean up a couple proxy test cases
* Oops, fix variable name
2017-05-02 00:11:10 -05:00
|
|
|
if got, want := repl.Replace("This path is '{path}'"), "This path is 'a/custom/path.php'"; got != want {
|
|
|
|
t.Errorf("{path} replacement failed; got '%s', want '%s'", got, want)
|
2017-03-11 16:59:47 -05:00
|
|
|
}
|
|
|
|
|
httpserver/all: Clean up and standardize request URL handling (#1633)
* httpserver/all: Clean up and standardize request URL handling
The HTTP server now always creates a context value on the request which
is a copy of the request's URL struct. It should not be modified by
middlewares, but it is safe to get the value out of the request and make
changes to it locally-scoped. Thus, the value in the context always
stores the original request URL information as it was received. Any
rewrites that happen will be to the request's URL field directly.
The HTTP server no longer cleans /sanitizes the request URL. It made too
many strong assumptions and ended up making a lot of middleware more
complicated, including upstream proxying (and fastcgi). To alleviate
this complexity, we no longer change the request URL. Middlewares are
responsible to access the disk safely by using http.Dir or, if not
actually opening files, they can use httpserver.SafePath().
I'm hoping this will address issues with #1624, #1584, #1582, and others.
* staticfiles: Fix test on Windows
@abiosoft: I still can't figure out exactly what this is for. 😅
* Use (potentially) changed URL for browse redirects, as before
* Use filepath.ToSlash, clean up a couple proxy test cases
* Oops, fix variable name
2017-05-02 00:11:10 -05:00
|
|
|
if got, want := repl.Replace("This path is {rewrite_path}"), "This path is /index.php"; got != want {
|
|
|
|
t.Errorf("{rewrite_path} replacement failed; got '%s', want '%s'", got, want)
|
2017-03-11 16:59:47 -05:00
|
|
|
}
|
httpserver/all: Clean up and standardize request URL handling (#1633)
* httpserver/all: Clean up and standardize request URL handling
The HTTP server now always creates a context value on the request which
is a copy of the request's URL struct. It should not be modified by
middlewares, but it is safe to get the value out of the request and make
changes to it locally-scoped. Thus, the value in the context always
stores the original request URL information as it was received. Any
rewrites that happen will be to the request's URL field directly.
The HTTP server no longer cleans /sanitizes the request URL. It made too
many strong assumptions and ended up making a lot of middleware more
complicated, including upstream proxying (and fastcgi). To alleviate
this complexity, we no longer change the request URL. Middlewares are
responsible to access the disk safely by using http.Dir or, if not
actually opening files, they can use httpserver.SafePath().
I'm hoping this will address issues with #1624, #1584, #1582, and others.
* staticfiles: Fix test on Windows
@abiosoft: I still can't figure out exactly what this is for. 😅
* Use (potentially) changed URL for browse redirects, as before
* Use filepath.ToSlash, clean up a couple proxy test cases
* Oops, fix variable name
2017-05-02 00:11:10 -05:00
|
|
|
if got, want := repl.Replace("This path is '{uri}'"), "This path is 'a/custom/path.php?key=value'"; got != want {
|
|
|
|
t.Errorf("{uri} replacement failed; got '%s', want '%s'", got, want)
|
2017-03-11 16:59:47 -05:00
|
|
|
}
|
|
|
|
|
httpserver/all: Clean up and standardize request URL handling (#1633)
* httpserver/all: Clean up and standardize request URL handling
The HTTP server now always creates a context value on the request which
is a copy of the request's URL struct. It should not be modified by
middlewares, but it is safe to get the value out of the request and make
changes to it locally-scoped. Thus, the value in the context always
stores the original request URL information as it was received. Any
rewrites that happen will be to the request's URL field directly.
The HTTP server no longer cleans /sanitizes the request URL. It made too
many strong assumptions and ended up making a lot of middleware more
complicated, including upstream proxying (and fastcgi). To alleviate
this complexity, we no longer change the request URL. Middlewares are
responsible to access the disk safely by using http.Dir or, if not
actually opening files, they can use httpserver.SafePath().
I'm hoping this will address issues with #1624, #1584, #1582, and others.
* staticfiles: Fix test on Windows
@abiosoft: I still can't figure out exactly what this is for. 😅
* Use (potentially) changed URL for browse redirects, as before
* Use filepath.ToSlash, clean up a couple proxy test cases
* Oops, fix variable name
2017-05-02 00:11:10 -05:00
|
|
|
if got, want := repl.Replace("This path is {rewrite_uri}"), "This path is /index.php?key=value"; got != want {
|
|
|
|
t.Errorf("{rewrite_uri} replacement failed; got '%s', want '%s'", got, want)
|
2017-03-11 16:59:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-08 11:14:53 -05:00
|
|
|
func TestRound(t *testing.T) {
|
|
|
|
var tests = map[time.Duration]time.Duration{
|
|
|
|
// 599.935µs -> 560µs
|
|
|
|
559935 * time.Nanosecond: 560 * time.Microsecond,
|
|
|
|
// 1.55ms -> 2ms
|
|
|
|
1550 * time.Microsecond: 2 * time.Millisecond,
|
|
|
|
// 1.5555s -> 1.556s
|
|
|
|
1555500 * time.Microsecond: 1556 * time.Millisecond,
|
|
|
|
// 1m2.0035s -> 1m2.004s
|
|
|
|
62003500 * time.Microsecond: 62004 * time.Millisecond,
|
|
|
|
}
|
|
|
|
|
|
|
|
for dur, expected := range tests {
|
|
|
|
rounded := roundDuration(dur)
|
|
|
|
if rounded != expected {
|
|
|
|
t.Errorf("Expected %v, Got %v", expected, rounded)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-05 23:02:45 -05:00
|
|
|
|
|
|
|
func TestMillisecondConverstion(t *testing.T) {
|
|
|
|
var testCases = map[time.Duration]int64{
|
|
|
|
2 * time.Second: 2000,
|
|
|
|
9039492 * time.Nanosecond: 9,
|
|
|
|
1000 * time.Microsecond: 1,
|
|
|
|
127 * time.Nanosecond: 0,
|
|
|
|
0 * time.Millisecond: 0,
|
|
|
|
255 * time.Millisecond: 255,
|
|
|
|
}
|
|
|
|
|
|
|
|
for dur, expected := range testCases {
|
|
|
|
numMillisecond := convertToMilliseconds(dur)
|
|
|
|
if numMillisecond != expected {
|
|
|
|
t.Errorf("Expected %v. Got %v", expected, numMillisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|