|
| 1 | +package datasource |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "reflect" |
| 10 | + "strings" |
| 11 | + "sync" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "deps.dev/util/maven" |
| 15 | +) |
| 16 | + |
| 17 | +type fakeMavenRegistry struct { |
| 18 | + mu sync.Mutex |
| 19 | + repository map[string]string // path -> response |
| 20 | +} |
| 21 | + |
| 22 | +func (f *fakeMavenRegistry) setResponse(path, response string) { |
| 23 | + f.mu.Lock() |
| 24 | + defer f.mu.Unlock() |
| 25 | + if f.repository == nil { |
| 26 | + f.repository = make(map[string]string) |
| 27 | + } |
| 28 | + f.repository[path] = response |
| 29 | +} |
| 30 | + |
| 31 | +func (f *fakeMavenRegistry) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 32 | + f.mu.Lock() |
| 33 | + resp, ok := f.repository[strings.TrimPrefix(r.URL.Path, "/")] |
| 34 | + f.mu.Unlock() |
| 35 | + if !ok { |
| 36 | + w.WriteHeader(http.StatusNotFound) |
| 37 | + resp = "not found" |
| 38 | + } |
| 39 | + if _, err := io.WriteString(w, resp); err != nil { |
| 40 | + log.Fatalf("WriteString: %v", err) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestGetProject(t *testing.T) { |
| 45 | + t.Parallel() |
| 46 | + |
| 47 | + fakeMaven := &fakeMavenRegistry{} |
| 48 | + srv := httptest.NewServer(fakeMaven) |
| 49 | + defer srv.Close() |
| 50 | + client := &MavenRegistryAPIClient{ |
| 51 | + Registry: srv.URL, |
| 52 | + } |
| 53 | + |
| 54 | + fakeMaven.setResponse("org/example/x.y.z/1.0.0/x.y.z-1.0.0.pom", ` |
| 55 | + <project> |
| 56 | + <groupId>org.example</groupId> |
| 57 | + <artifactId>x.y.z</artifactId> |
| 58 | + <version>1.0.0</version> |
| 59 | + </project> |
| 60 | + `) |
| 61 | + got, err := client.GetProject(context.Background(), "org.example", "x.y.z", "1.0.0") |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("failed to get Maven project %s:%s verion %s: %v", "org.example", "x.y.z", "1.0.0", err) |
| 64 | + } |
| 65 | + want := maven.Project{ |
| 66 | + ProjectKey: maven.ProjectKey{ |
| 67 | + GroupID: "org.example", |
| 68 | + ArtifactID: "x.y.z", |
| 69 | + Version: "1.0.0", |
| 70 | + }, |
| 71 | + } |
| 72 | + if !reflect.DeepEqual(got, want) { |
| 73 | + t.Errorf("GetProject(%s, %s, %s):\ngot %v\nwant %v\n", "org.example", "x.y.z", "1.0.0", got, want) |
| 74 | + } |
| 75 | +} |
0 commit comments