Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package srv // import "github.com/frioux/leatherman/internal/tool/srv" | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func Serve(args []string, _ io.Reader) error { | ||
dir := "." | ||
if len(args) > 1 { | ||
dir = args[1] | ||
} | ||
|
||
return serve(dir, os.Stderr) | ||
} | ||
|
||
func serve(dir string, log io.Writer) error { | ||
listener, err := net.Listen("tcp", ":0") | ||
if err != nil { | ||
return errors.Wrap(err, "net.Listen") | ||
} | ||
|
||
fmt.Fprintf(log, "Serving %s on %s\n", dir, listener.Addr()) | ||
|
||
return http.Serve(listener, http.FileServer(http.Dir(dir))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package srv | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"math/rand" | ||
"net/http" | ||
"os" | ||
"regexp" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestServe(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
go serve(".", buf) | ||
|
||
sock := regexp.MustCompile("^Serving . on (.+)\n") | ||
|
||
var resp *http.Response | ||
var err error | ||
for i := 1; i < 11; i++ { | ||
m := sock.FindStringSubmatch(buf.String()) | ||
if len(m) == 0 { | ||
time.Sleep(time.Millisecond * time.Duration(rand.Intn(i))) | ||
continue | ||
} | ||
|
||
resp, err = http.Get("http://" + m[1] + "/srv.go") | ||
if err != nil { | ||
t.Fatalf("Couldn't fetch srv.go: %s", err) | ||
} | ||
break | ||
} | ||
|
||
if resp == nil { | ||
t.Fatalf("couldn't get response from server within timeout") | ||
} | ||
|
||
f, err := os.Open("./srv.go") | ||
if err != nil { | ||
t.Fatalf("Couldn't open ./srv.go: %s", err) | ||
} | ||
expected, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
t.Fatalf("Couldn't read ./srv.go: %s", err) | ||
} | ||
|
||
got, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
t.Fatalf("Couldn't read response: %s", err) | ||
} | ||
|
||
if len(expected) == 0 { | ||
t.Fatal("Somehow got empty test case") | ||
} | ||
|
||
assert.Equal(t, expected, got) | ||
} |