I have a program written in Go which runs Git to connect to GitHub. I'm trying to write a (unit) test for this program, hence I want to set up my own HTTP server inside the test to mock out GitHub. Essentially, to locally run the simplest possible fake version of GitHub (a remote Git hosting site).
I thought that it would be enough to create a test Git repository and serve the directory over HTTP. However, this doesn't seem to work. Here's my attempt (written in Go) and the output. (Assume /my/local/testrepo is a directory on my machine that contains a Git repo).
package main
import (
"fmt"
"net/http"
"os/exec"
"time"
)
func main() {
// Set up Git server
http.Handle("/", http.FileServer(http.Dir("/my/local/testrepo")))
go http.ListenAndServe(":8080", nil)
time.Sleep(1 * time.Second)
out, err := exec.Command("git", "clone", "http://localhost:8080/").CombinedOutput()
if err != nil {
fmt.Printf("exec error: %v\n\n", err)
}
fmt.Println(string(out))
}
output:
exec error: exit status 128
Cloning into 'localhost'...
remote: 404 page not found
fatal: repository 'http://localhost:8080/' not found
How do I make this work?