Elffuss BraidHub

kikocis/lancedb public

main · 2.5 KiBRawPermalink
1// Copyright 2024 Lance Developers.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import * as http from "http";
16import { RequestListener } from "http";
17import { Connection, ConnectionOptions, connect } from "../lancedb";
18
19async function withMockDatabase(
20 listener: RequestListener,
21 callback: (db: Connection) => void,
22 connectionOptions?: ConnectionOptions,
23) {
24 const server = http.createServer(listener);
25 server.listen(8000);
26
27 const db = await connect(
28 "db://dev",
29 Object.assign(
30 {
31 apiKey: "fake",
32 hostOverride: "http://localhost:8000",
33 },
34 connectionOptions,
35 ),
36 );
37
38 try {
39 await callback(db);
40 } finally {
41 server.close();
42 }
43}
44
45describe("remote connection", () => {
46 it("should accept partial connection options", async () => {
47 await connect("db://test", {
48 apiKey: "fake",
49 clientConfig: {
50 timeoutConfig: { readTimeout: 5 },
51 retryConfig: { retries: 2 },
52 },
53 });
54 });
55
56 it("should pass down apiKey and userAgent", async () => {
57 await withMockDatabase(
58 (req, res) => {
59 expect(req.headers["x-api-key"]).toEqual("fake");
60 expect(req.headers["user-agent"]).toEqual(
61 `LanceDB-Node-Client/${process.env.npm_package_version}`,
62 );
63
64 const body = JSON.stringify({ tables: [] });
65 res.writeHead(200, { "Content-Type": "application/json" }).end(body);
66 },
67 async (db) => {
68 const tableNames = await db.tableNames();
69 expect(tableNames).toEqual([]);
70 },
71 );
72 });
73
74 it("allows customizing user agent", async () => {
75 await withMockDatabase(
76 (req, res) => {
77 expect(req.headers["user-agent"]).toEqual("MyApp/1.0");
78
79 const body = JSON.stringify({ tables: [] });
80 res.writeHead(200, { "Content-Type": "application/json" }).end(body);
81 },
82 async (db) => {
83 const tableNames = await db.tableNames();
84 expect(tableNames).toEqual([]);
85 },
86 {
87 clientConfig: {
88 userAgent: "MyApp/1.0",
89 },
90 },
91 );
92 });
93});