forked from joewalnes/websocketd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
launcher_test.go
102 lines (89 loc) · 2.15 KB
/
launcher_test.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2013 Joe Walnes and the websocketd team.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package libwebsocketd
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestParsePathWithScriptDir(t *testing.T) {
baseDir, _ := ioutil.TempDir("", "websockets")
scriptDir := filepath.Join(baseDir, "foo", "bar")
scriptPath := filepath.Join(scriptDir, "baz.sh")
defer os.RemoveAll(baseDir)
if err := os.MkdirAll(scriptDir, os.ModePerm); err != nil {
t.Error("could not create ", scriptDir)
}
if _, err := os.Create(scriptPath); err != nil {
t.Error("could not create ", scriptPath)
}
config := new(Config)
config.UsingScriptDir = true
config.ScriptDir = baseDir
var res *URLInfo
var err error
// simple url
res, err = parsePath("/foo/bar/baz.sh", config)
if err != nil {
t.Error(err)
}
if res.ScriptPath != "/foo/bar/baz.sh" {
t.Error("scriptPath")
}
if res.PathInfo != "" {
t.Error("pathInfo")
}
if res.FilePath != scriptPath {
t.Error("filePath")
}
// url with extra path info
res, err = parsePath("/foo/bar/baz.sh/some/extra/stuff", config)
if err != nil {
t.Error(err)
}
if res.ScriptPath != "/foo/bar/baz.sh" {
t.Error("scriptPath")
}
if res.PathInfo != "/some/extra/stuff" {
t.Error("pathInfo")
}
if res.FilePath != scriptPath {
t.Error("filePath")
}
// non-existing file
_, err = parsePath("/foo/bar/bang.sh", config)
if err == nil {
t.Error("non-existing file should fail")
}
if err != ScriptNotFoundError {
t.Error("should fail with script not found")
}
// non-existing dir
_, err = parsePath("/hoohar/bang.sh", config)
if err == nil {
t.Error("non-existing dir should fail")
}
if err != ScriptNotFoundError {
t.Error("should fail with script not found")
}
}
func TestParsePathExplicitScript(t *testing.T) {
config := new(Config)
config.UsingScriptDir = false
res, err := parsePath("/some/path", config)
if err != nil {
t.Error(err)
}
if res.ScriptPath != "/" {
t.Error("scriptPath")
}
if res.PathInfo != "/some/path" {
t.Error("pathInfo")
}
if res.FilePath != "" {
t.Error("filePath")
}
}