Skip to content

Commit b03c2a6

Browse files
committed
testsuite: added http server helper
1 parent 373b664 commit b03c2a6

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

internal/integrationtest/http.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package integrationtest
17+
18+
import (
19+
"fmt"
20+
"net/http"
21+
"net/url"
22+
"testing"
23+
24+
"github.com/arduino/go-paths-helper"
25+
"github.com/stretchr/testify/require"
26+
)
27+
28+
// HTTPServeFile spawn an http server that serve a single file. The server
29+
// is started on the given port. The URL to the file and a cleanup function are returned.
30+
func HTTPServeFile(t *testing.T, port uint16, path *paths.Path) (*url.URL, func()) {
31+
mux := http.NewServeMux()
32+
mux.HandleFunc("/"+path.Base(), func(w http.ResponseWriter, r *http.Request) {
33+
http.ServeFile(w, r, path.String())
34+
})
35+
server := &http.Server{
36+
Addr: fmt.Sprintf(":%d", port),
37+
Handler: mux,
38+
}
39+
40+
fileUrl, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d/%s", port, path.Base()))
41+
require.NoError(t, err)
42+
43+
go func() {
44+
err := server.ListenAndServe()
45+
require.Equal(t, err, http.ErrServerClosed)
46+
}()
47+
48+
return fileUrl, func() { server.Close() }
49+
}

0 commit comments

Comments
 (0)