-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #351 from susnux/feat/implement-search
feat: Implement `SEARCH` according to `rfc5323`
- Loading branch information
Showing
8 changed files
with
319 additions
and
15 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,38 @@ | ||
import { parseSearch, parseXML } from "../tools/dav.js"; | ||
import { joinURL } from "../tools/url.js"; | ||
import { encodePath } from "../tools/path.js"; | ||
import { request, prepareRequestOptions } from "../request.js"; | ||
import { handleResponseCode, processResponsePayload } from "../response.js"; | ||
import { | ||
SearchResult, | ||
ResponseDataDetailed, | ||
SearchOptions, | ||
WebDAVClientContext | ||
} from "../types.js"; | ||
|
||
export async function getSearch( | ||
context: WebDAVClientContext, | ||
searchArbiter: string, | ||
options: SearchOptions = {} | ||
): Promise<SearchResult | ResponseDataDetailed<SearchResult>> { | ||
const { details: isDetailed = false } = options; | ||
const requestOptions = prepareRequestOptions( | ||
{ | ||
url: joinURL(context.remoteURL, encodePath(searchArbiter)), | ||
method: "SEARCH", | ||
headers: { | ||
Accept: "text/plain,application/xml", | ||
// Ensure a Content-Type header is set was this is required by e.g. sabre/dav | ||
"Content-Type": context.headers["Content-Type"] || "application/xml; charset=utf-8" | ||
} | ||
}, | ||
context, | ||
options | ||
); | ||
const response = await request(requestOptions); | ||
handleResponseCode(context, response); | ||
const responseText = await response.text(); | ||
const responseData = await parseXML(responseText); | ||
const results = parseSearch(responseData, searchArbiter, isDetailed); | ||
return processResponsePayload(response, results, isDetailed); | ||
} |
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,95 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import { fileURLToPath } from "url"; | ||
import { expect } from "chai"; | ||
import { | ||
SERVER_PASSWORD, | ||
SERVER_PORT, | ||
SERVER_USERNAME, | ||
clean, | ||
createWebDAVClient, | ||
createWebDAVServer, | ||
restoreRequests, | ||
returnFakeResponse, | ||
useRequestSpy | ||
} from "../../helpers.node.js"; | ||
import { ResponseDataDetailed, SearchResult, WebDAVClient } from "../../../source/types.js"; | ||
|
||
const dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
function useTruncatedSearchResults() { | ||
returnFakeResponse( | ||
fs.readFileSync(path.resolve(dirname, "../../responses/search-truncated.xml"), "utf8") | ||
); | ||
} | ||
|
||
function useFullSearchResults() { | ||
returnFakeResponse( | ||
fs.readFileSync(path.resolve(dirname, "../../responses/search-full-success.xml"), "utf8") | ||
); | ||
} | ||
|
||
const searchRequest = `<?xml version="1.0" encoding="UTF-8"?> | ||
<d:searchrequest xmlns:d="DAV:" xmlns:f="http://example.com/foo"> | ||
<f:natural-language-query> | ||
Find files changed 2023-08-03 | ||
</f:natural-language-query> | ||
</d:searchrequest> | ||
`; | ||
|
||
describe("search", function () { | ||
let client: WebDAVClient; | ||
beforeEach(function () { | ||
// fake client, not actually used when mocking responses | ||
client = createWebDAVClient(`http://localhost:${SERVER_PORT}/webdav/server`, { | ||
username: SERVER_USERNAME, | ||
password: SERVER_PASSWORD | ||
}); | ||
clean(); | ||
this.server = createWebDAVServer(); | ||
this.requestSpy = useRequestSpy(); | ||
return this.server.start(); | ||
}); | ||
|
||
afterEach(function () { | ||
restoreRequests(); | ||
return this.server.stop(); | ||
}); | ||
|
||
it("returns full search response", function () { | ||
useFullSearchResults(); | ||
return client.search("/", { data: searchRequest }).then(function (result) { | ||
expect(result).to.be.an("object"); | ||
expect(result).to.have.property("truncated", false); | ||
expect(result).to.have.property("results"); | ||
expect((result as SearchResult).results.length).to.equal(2); | ||
expect((result as SearchResult).results[0].basename).to.equal("first-file.md"); | ||
expect((result as SearchResult).results[1].basename).to.equal("second file.txt"); | ||
}); | ||
}); | ||
|
||
it("returns full detailed search response", function () { | ||
useFullSearchResults(); | ||
return client.search("/", { data: searchRequest, details: true }).then(function (result) { | ||
expect(result).to.be.an("object"); | ||
result = (result as ResponseDataDetailed<SearchResult>).data; | ||
expect(result).to.be.an("object"); | ||
expect(result).to.have.property("truncated", false); | ||
expect(result).to.have.property("results"); | ||
expect(result.results.length).to.equal(2); | ||
expect(result.results[0].basename).to.equal("first-file.md"); | ||
expect(result.results[0].props.getcontenttype).to.equal("text/markdown"); | ||
}); | ||
}); | ||
|
||
it("returns truncated search response", function () { | ||
useTruncatedSearchResults(); | ||
return client.search("/", { data: searchRequest }).then(function (result) { | ||
expect(result).to.be.an("object"); | ||
expect(result).to.have.property("truncated", true); | ||
expect(result).to.have.property("results"); | ||
expect((result as SearchResult).results.length).to.equal(1); | ||
expect((result as SearchResult).results[0].basename).to.equal("first-file.md"); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.