2022-02-27 04:07:39 +02:00
|
|
|
import Resolver from '../../src/remote/activitypub/resolver.js';
|
|
|
|
import { IObject } from '../../src/remote/activitypub/type.js';
|
2021-07-10 17:14:57 +03:00
|
|
|
|
|
|
|
type MockResponse = {
|
|
|
|
type: string;
|
|
|
|
content: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class MockResolver extends Resolver {
|
|
|
|
private _rs = new Map<string, MockResponse>();
|
|
|
|
public async _register(uri: string, content: string | Record<string, any>, type = 'application/activity+json') {
|
|
|
|
this._rs.set(uri, {
|
|
|
|
type,
|
2022-05-21 16:21:41 +03:00
|
|
|
content: typeof content === 'string' ? content : JSON.stringify(content),
|
2021-07-10 17:14:57 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async resolve(value: string | IObject): Promise<IObject> {
|
|
|
|
if (typeof value !== 'string') return value;
|
|
|
|
|
|
|
|
const r = this._rs.get(value);
|
|
|
|
|
|
|
|
if (!r) {
|
|
|
|
throw {
|
2022-05-21 16:21:41 +03:00
|
|
|
name: 'StatusError',
|
2021-07-10 17:14:57 +03:00
|
|
|
statusCode: 404,
|
2022-05-21 16:21:41 +03:00
|
|
|
message: 'Not registed for mock',
|
2021-07-10 17:14:57 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const object = JSON.parse(r.content);
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
}
|