Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/lu/src/parser/lubuild/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const exception = require('./../utils/exception')
const Luis = require('./../luis/luis')

const rateLimitErrorCode = 429
const absoluteUrlPattern = /^https?:\/\//i

export class LuBuildCore {
private readonly client: any
Expand All @@ -27,6 +28,11 @@ export class LuBuildCore {
this.retryCount = retryCount
this.retryDuration = retryDuration

// check endpoint is absolute or not
if (!absoluteUrlPattern.test(endpoint)) {
throw (new exception(retCode.errorCode.INVALID_URI, `Only absolute URLs are supported. "${endpoint}" is not an absolute LUIS endpoint URL.`))
}

// new luis api client
const creds = new CognitiveServicesCredentials(subscriptionKey)
this.client = new LUISAuthoringClient(creds, endpoint)
Expand Down
7 changes: 7 additions & 0 deletions packages/lu/src/parser/qnabuild/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ const exception = require('./../utils/exception')
const {ServiceBase} = require('./serviceBase')
const NEWLINE = require('os').EOL

const absoluteUrlPattern = /^https?:\/\//i

export class QnaBuildCore {
private readonly service: any

constructor(subscriptionkey: string, endpoint: string) {
// check endpoint is absolute or not
if (!absoluteUrlPattern.test(endpoint)) {
throw (new exception(retCode.errorCode.INVALID_URI, `Only absolute URLs are supported. "${endpoint}" is not an absolute qnamaker endpoint URL.`))
}

this.service = new ServiceBase(endpoint, subscriptionkey)
}

Expand Down
34 changes: 34 additions & 0 deletions packages/lu/test/parser/lubuild/lubuild.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,38 @@ describe('builder: loadContents function can catch invalid import exceptions suc
assert.isTrue(e.text.includes("Invalid LU file") && e.text.includes(`[ERROR] URI: "https://xxxxx/1.lu" appears to be invalid.`))
}
})
})

describe('builder: build function can catch relative endpoint exception successfully', () => {
it('should throw exception for non absolute endpoint', async () => {
const builder = new Builder(() => { })
try {
await builder.build(
[new luObject('# Greeting')],
undefined,
'f8c64e2a-1111-3a09-8f78-39d7adc76ec5',
'http:fsd'
)

assert.fail("Relative endpoint exception is not thrown.")
} catch (e) {
assert.equal(e.text, `Only absolute URLs are supported. "http:fsd" is not an absolute LUIS endpoint URL.`)
}
})

it('should throw exception for non absolute endpoint', async () => {
const builder = new Builder(() => { })
try {
await builder.build(
[new luObject('# Greeting')],
undefined,
'f8c64e2a-1111-3a09-8f78-39d7adc76ec5',
'fsd'
)

assert.fail("Relative endpoint exception is not thrown.")
} catch (e) {
assert.equal(e.text, `Only absolute URLs are supported. "fsd" is not an absolute LUIS endpoint URL.`)
}
})
})
40 changes: 37 additions & 3 deletions packages/lu/test/parser/qnabuild/qnabuild.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const uuidv1 = require('uuid/v1')
const path = require('path')
const NEWLINE = require('os').EOL
const Builder = require('../../../src/parser/qnabuild/builder').Builder
const luObject = require('../../../src/parser/lu/lu')
const luOptions = require('../../../src/parser/lu/luOptions')
const qnaObject = require('../../../src/parser/lu/qna')
const qnaOptions = require('../../../src/parser/lu/qnaOptions')
const txtfile = require('../../../src/parser/lufile/read-text-file');

const rootDir = path.join(__dirname, './../../fixtures/testcases/import-resolver/qna-import-resolver')
Expand Down Expand Up @@ -271,7 +271,7 @@ describe('builder: loadContents function can resolve import files with customize
file.filePath = file.filePath.slice(0, file.filePath.length - 3) + "en-us.qna"
}

luObjects.push(new luObject(txtfile.readSync(file.filePath), new luOptions(file.filePath, file.includeInCollate)))
luObjects.push(new qnaObject(txtfile.readSync(file.filePath), new qnaOptions(file.filePath, file.includeInCollate)))
}
return luObjects
};
Expand All @@ -290,4 +290,38 @@ describe('builder: loadContents function can resolve import files with customize
assert.isTrue(result.qnaContents[0].content.includes(
`!# @qna.pair.source = custom editorial${NEWLINE}${NEWLINE}## ? help${NEWLINE}- could you help${NEWLINE}${NEWLINE}\`\`\`markdown${NEWLINE}help answer${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}> !# @qna.pair.source = custom editorial${NEWLINE}${NEWLINE}## ? welcome${NEWLINE}${NEWLINE}\`\`\`markdown${NEWLINE}welcome here${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}> !# @qna.pair.source = custom editorial${NEWLINE}${NEWLINE}## ? cancel${NEWLINE}${NEWLINE}\`\`\`markdown${NEWLINE}cancel the task${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}> !# @qna.pair.source = custom editorial${NEWLINE}${NEWLINE}## ? stop${NEWLINE}${NEWLINE}\`\`\`markdown${NEWLINE}stop that${NEWLINE}\`\`\``))
})
})

describe('builder: build function can catch relative endpoint exception successfully', () => {
it('should throw exception for non absolute endpoint', async () => {
const builder = new Builder(() => { })
try {
await builder.build(
[new qnaObject(`# ? Greeting${NEWLINE}\`\`\`${NEWLINE}hello${NEWLINE}\`\`\``)],
undefined,
'f8c64e2a-1111-3a09-8f78-39d7adc76ec5',
'http:fsd'
)

assert.fail("Relative endpoint exception is not thrown.")
} catch (e) {
assert.equal(e.text, `Only absolute URLs are supported. "http:fsd" is not an absolute qnamaker endpoint URL.`)
}
})

it('should throw exception for non absolute endpoint', async () => {
const builder = new Builder(() => { })
try {
await builder.build(
[new qnaObject(`# ? Greeting${NEWLINE}\`\`\`${NEWLINE}hello${NEWLINE}\`\`\``)],
undefined,
'f8c64e2a-1111-3a09-8f78-39d7adc76ec5',
'fsd'
)

assert.fail("Relative endpoint exception is not thrown.")
} catch (e) {
assert.equal(e.text, `Only absolute URLs are supported. "fsd" is not an absolute qnamaker endpoint URL.`)
}
})
})